Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Strange cache behaviour after clear
Strange cache behaviour after clear [message #755117] Mon, 07 November 2011 08:03 Go to next message
Tom Bailey is currently offline Tom Bailey
Messages: 4
Registered: November 2011
Junior Member
We are using glassfish 3.1.1 (with eclipselink 2.3) and are having a strange problem with the cache on the entity manager.
Basically we want to detach entities before sending them to subsystems. It is the case (quite often) that the same entity can be changed twice within one transaction (by two different subsystems) and this multiple update of detached entities seems to expose an issue with the entity manager cache.
A simple example that exposes the problem is in the code below:
DataObject dataObject = entityManager.find(DataObject.class, dataObjectId);
entityManager.clear();
// First update
dataObject.setNotes("Notes 1");
entityManager.merge(dataObject);
// Simulate query implicit flush
entityManager.flush();
// Re-retrieve the entity
dataObject = entityManager.find(DataObject.class, dataObjectId);
// Detach again
entityManager.clear();
// Next update
dataObject.setNotes("Notes 2");
entityManager.merge(dataObject);
This code when run many times will alternately persist "Notes 1" to the database followed by "Notes 2" followed by "Notes 1" etc. However, if the code is changed so that the number after Notes increments every time then the correct value is always persisted. Therefore, it looks like the cache isn't updated after the first merge, so if the second merge has the same value as the object in cache then no merge takes place (leaving "Notes 1" on the object being persisted to the database).
The reason for the flush is because a query between the two updates would invoke an implicit flush. Without the flush the problem does not occur, so the flush seems to clear the entity manager cache back to the original state, rather than the state flushed to the database.

Adding a cache evict fixes the code:
DataObject dataObject = entityManager.find(DataObject.class, dataObjectId);
entityManager.clear();
// First update
dataObject.setNotes("Notes 1");
entityManager.merge(dataObject);
entityManager.getEntityManagerFactory().getCache().evict(DataObject.class, dataObject.getId());
// Simulate query implicit flush
entityManager.flush();
// Re-retrieve the entity
dataObject = entityManager.find(DataObject.class, dataObjectId);
// Detach again
entityManager.clear();
// Next update
dataObject.setNotes("Notes 2");
entityManager.merge(dataObject);
but it makes the system a lot slower to run and adds a requirement that the developer remembers to do an evict after every merge.

I assume this is a bug, but if anyone believes this is an invalid use of the entity manager then please reply.
Re: Strange cache behaviour after clear [message #755238 is a reply to message #755117] Mon, 07 November 2011 13:35 Go to previous messageGo to next message
James is currently offline James
Messages: 272
Registered: July 2009
Senior Member
The cache should not be updated after the merge, it can only be updated after the commit. But the second find should not hit the cache, this seems to be a bug, please log the bug.

If you remove the clear(), then the issue will not occur (and calling clear in the middle of a transaction is not normally a good idea). Also you could call refresh() instead of find() (or call find() with the refresh property).

My guess is that it would not occur with a Query, only with find().

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Re: Strange cache behaviour after clear [message #755367 is a reply to message #755117] Tue, 08 November 2011 04:39 Go to previous message
Tom Bailey is currently offline Tom Bailey
Messages: 4
Registered: November 2011
Junior Member
Thanks James. Have raised defect 363136
<I'm not yet allowed to post links, so readers of this post will need to find the bugzilla system themselves!>
Previous Topic:Strange cache behaviour after clear
Next Topic:How to get return code from a stored procedure using EclipseLink
Goto Forum:
  


Current Time: Wed Aug 27 13:18:21 EDT 2014

Powered by FUDForum. Page generated in 0.19096 seconds