Search code examples
javahibernatecachingframeworksdirty-data

Java - Framework to detect if Object has changed / dirty detection mechanism


Dear all I am currently implementing a java client which is using quite heavily third party webservices. In order to gain performance I just like to call webservices only in case the objects on my client side has been modified (become dirty).

Instead of writing an own kind of framework which is able to detect if an object is dirty, exists there any open / generic framework which can be reused and is not bind to its core product (e.g. hibernate)?


Solution

  • I presume with object you don't mean a single scalar value but a bean.

    Technically you can do all sorts of fancy stuff to detect the bean mutation, for example changing the byte code and add some code whenever the object field is updated.

    Another option is to keep a copy of the old bean instance and compare it. So actually the problem reduces to comparing two beans, which was asked here: how to generically compare entire java beans? Probably you find more, there are a lot of frameworks dealing with beans in general.

    However, since you call webservices, you must have a mechanism to serialize your objects. You could use the serialized form of the old and new object to compare for identity before sending the update request.

    Change notification: I don't recommend attaching change listeners to every bean. This might change your general performance and introduce memory leaks. There is also a transaction problem: If more then one bean property is updated, when is the update of a bean completed? So you need an explicit call after the mutation anyways.

    Note to myself and other caching guys: Actually this is the use case to provide a method like Cache.putIfNotEquals(key, value) on a cache, which is not much efford. The cache stores the previous value already and it does only call the cache writer (in a write through setup) if the value changed.