|How can I check PersistenceContexts for equality [message #757045]
||Wed, 16 November 2011 03:50
| Piotr Nowicki
Registered: November 2011
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
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.
|Re: How can I check PersistenceContexts for equality [message #757806 is a reply to message #757045]
||Mon, 21 November 2011 13:36
| James Sutherland
Registered: July 2009
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
Powered by FUDForum
. Page generated in 0.02446 seconds