Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Strange cache behaviour after clear(Strange cache behaviour after a clear and flush of entity manager)
Strange cache behaviour after clear [message #755116] 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 #755235 is a reply to message #755116] Mon, 07 November 2011 13:35 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
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 : Wiki : Book : Blog : Twitter
Re: Strange cache behaviour after clear [message #755365 is a reply to message #755116] 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:lazy loading problem
Next Topic:Strange cache behaviour after clear
Goto Forum:
  


Current Time: Thu Jul 24 11:28:55 EDT 2014

Powered by FUDForum. Page generated in 0.01666 seconds