org.modeshape.connector.store.jpa
Class EntityManagers

java.lang.Object
  extended by org.modeshape.connector.store.jpa.EntityManagers

@ThreadSafe
public class EntityManagers
extends Object

Utility class that owns an EntityManagerFactory instance and that provides references to EntityManager instances while providing the ability to properly clean up all resources by closing the EntityManager and EntityManagerFactory objects when no longer needed.

This class is instantiated by the JpaSource and passed to the RepositoryConnection objects, which use this class to obtain an EntityManager. When the JPA connection object is closed, it returns the EntityManager to this object. Because this class maintains a count of the EntityManager references handed out, the last EntityManager to be returned will cause the EntityManagerFactory to be closed.

This class does put the EntityManager implementations inside a HashMap, and therefore does expect that the EntityManager uses object equality for equals.


Method Summary
 void checkin(javax.persistence.EntityManager manager)
          Return an EntityManager when it is no longer needed.
 javax.persistence.EntityManager checkout()
          Check out an EntityManager instance.
 void close()
           
 void closeNow()
          For all opened EntityManager instances to be closed immediately, even if they are checked out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

checkout

public javax.persistence.EntityManager checkout()
Check out an EntityManager instance. The resulting manager must be returned with checkin(EntityManager). In fact, for every checkout() call, there must be a corresponding checkin(EntityManager) call with the checked out EntityManager.

Note that this class may return the same EntityManager for multiple calls, but this should not matter to the caller.

Returns:
the entity manager; never null

checkin

public void checkin(javax.persistence.EntityManager manager)
Return an EntityManager when it is no longer needed. This method should be called once supplying the same EntityManager instance returned from a checkout() call. In fact, for every checkout() call, there must be a corresponding checkin(EntityManager) call with the checked out EntityManager.

If this is the last reference to the EntityManager instance, it will be closed.

Parameters:
manager - the entity manager; may not be null and must be the result of a prior checkout() call

close

public void close()

closeNow

public void closeNow()
For all opened EntityManager instances to be closed immediately, even if they are checked out. This should be called with caution, since all EntityManager and EntityManagerFactory instances will be closed when no longer used - as long as the checkout/checkin pattern is always used.



Copyright © 2008-2011 JBoss, a division of Red Hat. All Rights Reserved.