Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » entitymanager.close() doesn't destroy persistence context in SE application
entitymanager.close() doesn't destroy persistence context in SE application [message #523475] Fri, 26 March 2010 15:02 Go to next message
Paul Bakker is currently offline Paul BakkerFriend
Messages: 8
Registered: March 2010
Junior Member
Closing an EntityManager should result in destroying the persistence context according the specs, but this doesn't seem to be the behavior in a SE application.

The following example results in only one query to the database, instead of two.

EntityManagerFactory factory= Persistence.createEntityManagerFactory("testjpa");			

EntityManager em;

em=factory.createEntityManager();			

Contact peterFromDB=em.find(Contact.class, 1);
em.close();

em=factory.createEntityManager();
Contact peterFromDB=em.find(Contact.class, 1);
em.close();

factory.close();


The example does the following:
-Create an EntityManagerFactory.
-Create an EntityManager
-Do a find
-Close the EntityManager (the persistence context should be gone now...)
-Create a new EntityManager
-Do a find again (this should just be new query to the db, there is no "cache" any more).

There is no second level cache configured. From the specs I understand this behavior is wrong.

Quote:

The EntityManager.close method closes an entity manager to release its persistence context and other resources.



It happens both with EclipseLink 1.2 and 2.0.

Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523484 is a reply to message #523475] Fri, 26 March 2010 10:26 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
What happens if you add this option as the second parameter when configuring the EMF?

Map<String, Object> lOptions = new HashMap<String, Object>();
lOptions.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "false");
EntityManagerFactory lEntityManagerFactory = Persistence.createEntityManagerFactory("testjpa", lOptions);
Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523488 is a reply to message #523484] Fri, 26 March 2010 15:27 Go to previous messageGo to next message
Paul Bakker is currently offline Paul BakkerFriend
Messages: 8
Registered: March 2010
Junior Member
With this option set it works as expected (two queries). To my opinion this is not according to the JPA specs, allthough it might be a useful optimization.

Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523501 is a reply to message #523488] Fri, 26 March 2010 15:45 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
> With this option set it works as expected (two queries). To my opinion
> this is not according to the JPA specs, allthough it might be a useful
> optimization.

I also found that with a standalone application (I have a swing app) this setting is unwanted. But with a single channel access, like a webapplication, this will give some serious speed benefits. So it is a useful feature, but maybe it should be off by default, because it produces unexpected results.

Tom
Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523502 is a reply to message #523501] Fri, 26 March 2010 16:02 Go to previous messageGo to next message
Paul Bakker is currently offline Paul BakkerFriend
Messages: 8
Registered: March 2010
Junior Member
I agree, it might lead to stale data for example. The downside of setting it to false by default is that most people will probably not set it to true, while it might be useful for most applications.

Because it's not really according to the specs I still believe it should be false anyway however.
Thanks for the fast answers btw Smile
Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523611 is a reply to message #523502] Sat, 27 March 2010 07:32 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
> Thanks for the fast answers btw :)

No problem.
I have a question pending here myself and had to keep an eye if anyone had responded. :-)
Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523739 is a reply to message #523475] Mon, 29 March 2010 06:41 Go to previous messageGo to next message
Paul Bakker is currently offline Paul BakkerFriend
Messages: 8
Registered: March 2010
Junior Member
I filed a bug for the issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=307342.
Re: entitymanager.close() doesn't destroy persistence context in SE application [message #523832 is a reply to message #523739] Mon, 29 March 2010 14:46 Go to previous message
Gordon Yorke is currently offline Gordon YorkeFriend
Messages: 77
Registered: July 2009
Member
In the provided example the Contact is being loaded from the shared cache and registered within the new Persistence Context. The original Persistence Context has been destroyed. You can verify this by keeping the reference to the original Context instance after closing the Persistence Context and changing an attribute of this instance. You will be able to verify that the subsequently found instance does not have this change.

You can turn EclipseLink caching off using the mentioned Persistence Unit property or through the JPA 2.0 cache settings and API however be aware that there may be a performance cost in disabling the cache.
Previous Topic:How to integrate EclipseLink into existing Client/Server application ?
Next Topic:Bug in NOT MEMBER OF translation?
Goto Forum:
  


Current Time: Sat Dec 20 12:23:37 GMT 2014

Powered by FUDForum. Page generated in 0.01740 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software