We've created a caching layer to our J2EE-application. In this instance we use Ehcache. This has created a few challenges.
Let's take this example.
OrderItem orderitem = cache.getOrderItemByID("id");
OrderItem old_orderitem = cache.getOrderItemID("id");
orderitem.setStatus(1);
old_orderitem.setStatus(2);
If we're not carefull, any changes made to either of those objects will affect the other (they reference the same object). Saving orderitem back to the dabase would make it have status=2
How would we solve this the best way?
We've tried making a .copyObject() method to each object. Which just creates a new object and sets all the values. But this doesn't seem like a good solution.
The example was just to illustrate. The code is far more complex than that, but the result is the same.
**********************Update 15.07.2010 **************************************************
In EHCache 2 there are some options to to turn on copyRead() and copyWrite(). And this solves all my problems :)