Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » How can I check PersistenceContexts for equality
How can I check PersistenceContexts for equality [message #757045] Wed, 16 November 2011 03:50 Go to next message
Piotr Nowicki is currently offline Piotr Nowicki
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Howdy Guys,

Is there a way to check if two EntityManagers instances represents the same PersistenceContext (container-managed, transaction-scoped) in EclipseLink?

Here is a paragraph from the JPA 2.0 specification:

7.8.2 Container Managed Persistence Contexts

Quote:
When operating with a third-party persistence provider, the container uses the contracts defined in section 7.9 to create and destroy container-managed persistence contexts. It is undefined whether a new entity manager instance is created for every persistence context, or whether entity manager instances are sometimes reused. Exactly how the container maintains the association between persistence context and JTA transaction is not defined.


If I understand it correctly, the JPA specification doesn't mandates if single EntityManager can be reused to represent multiple PersistenceContexts or each EntityManager instance will represents only one PersistenceContext and at the end it'll be released.

My question is - how it's in the EclipseLink. Does it reuse the EntityManager? Can I do naive 'System.out.println(em)' and check the EntityManager default toString() value for PersistenceContexts equality?

Or maybe it's not the EclipseLink responsibility to create/reuse the EntityManagers but the Application Server?

Thanks a lot for any clarifications.

This question has been also asked on StackOverflow and Javaranch - without any luck.

Cheers,
Piotr
Re: How can I check PersistenceContexts for equality [message #757806 is a reply to message #757045] Mon, 21 November 2011 13:36 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You EntityManager can either be JTA managed, or RESOURCE_LOCAL unmanaged.

If it is unmanaged, then every time you call createEntityManager, you will get a new EntityManager.

If it is JTA managed, then the EJB container is responsible for creating the EntityManager instance.
The way EJB servers do this is they wrap the real EclipseLink EntityManager in there own proxy. Every transaction will get a different EclipseLink EntityManager, and entity managers should reference the same EclipseLink EntityManager in the same transaction.

In WebLogic and Glassfish I believe a new EclipseLink EntityManager is created each transaction, and thrown away at the end.

In WebSphere I believe it does some caching of the EntityManager in some sort of pool and just calls clear() on them and then puts them back into the pool.

I do not recommend pooling EntityManagers.



James : Wiki : Book : Blog : Twitter
Re: How can I check PersistenceContexts for equality [message #758380 is a reply to message #757806] Tue, 22 November 2011 18:17 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr Nowicki
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Thanks a lot James!

You've provided me with a lot of interesting information Smile

I would just like to ask - when you've said that:

James wrote on Mon, 21 November 2011 13:36
In WebLogic and Glassfish I believe a new EclipseLink EntityManager is created each transaction, and thrown away at the end.

In WebSphere I believe it does some caching of the EntityManager in some sort of pool and just calls clear() on them and then puts them back into the pool.

I do not recommend pooling EntityManagers.


by EntityManager and it's pooling/cache'ing - you referred to the Server's proxy for EntityManager or the JPA-Provider EntityManager itself?

Thanks a lot once again!
Re: How can I check PersistenceContexts for equality [message #758813 is a reply to message #758380] Thu, 24 November 2011 11:01 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 provider's EntityManager. I don't think anything is gained in trying to pool and reuse one, versus creating a new one each time. Pooling just add complexity, potential concurrency issues, and the possibility of left over state, and access to old object getting new state.

James : Wiki : Book : Blog : Twitter
Re: How can I check PersistenceContexts for equality [message #758825 is a reply to message #758813] Thu, 24 November 2011 11:17 Go to previous message
Piotr Nowicki is currently offline Piotr Nowicki
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Much obliged for all these information and your time Smile
Re: How can I check PersistenceContexts for equality [message #758827 is a reply to message #758813] Thu, 24 November 2011 11:17 Go to previous message
Piotr Nowicki is currently offline Piotr Nowicki
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Much obliged for all these information and your time :)
Previous Topic:persist a collection in order
Next Topic:Date field is not updated
Goto Forum:
  


Current Time: Wed Apr 16 03:31:14 EDT 2014

Powered by FUDForum. Page generated in 0.04279 seconds