org.apache.openejb.persistence
Class JtaEntityManagerRegistry

java.lang.Object
  extended by org.apache.openejb.persistence.JtaEntityManagerRegistry

public class JtaEntityManagerRegistry
extends java.lang.Object

The JtaEntityManagerRegistry tracks JTA entity managers for transation and extended scoped entity managers. A signle instance of this object should be created and shared by all JtaEntityManagers in the server instance. Failure to do this will result in multiple entity managers being created for a single persistence until, and that will result in cache incoherence.


Constructor Summary
JtaEntityManagerRegistry(javax.transaction.TransactionSynchronizationRegistry transactionSynchronizationRegistry)
          Creates a JtaEntityManagerRegistry using the specified transactionSynchronizationRegistry for the registry if transaction associated entity managers.
 
Method Summary
 void addEntityManagers(java.lang.String deploymentId, java.lang.Object primaryKey, java.util.Map<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager> entityManagers)
          Adds the entity managers for the specified component to the registry.
 javax.persistence.EntityManager getEntityManager(javax.persistence.EntityManagerFactory entityManagerFactory, java.util.Map properties, boolean extended)
          Gets an entity manager instance from the transaction registry, extended regitry or for a transaction scoped entity manager, creates a new one when an exisitng instance is not found.
 javax.persistence.EntityManager getInheritedEntityManager(javax.persistence.EntityManagerFactory entityManagerFactory)
          Gets an exiting extended entity manager created by a component down the call stack.
 boolean isTransactionActive()
          Is a transaction active?
 void removeEntityManagers(java.lang.String deploymentId, java.lang.Object primaryKey)
          Removed the registered entity managers for the specified component.
 void transactionStarted(java.lang.String deploymentId, java.lang.Object primaryKey)
          Notifies the registry that a user transaction has been started or the specified component.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JtaEntityManagerRegistry

public JtaEntityManagerRegistry(javax.transaction.TransactionSynchronizationRegistry transactionSynchronizationRegistry)
Creates a JtaEntityManagerRegistry using the specified transactionSynchronizationRegistry for the registry if transaction associated entity managers.

Method Detail

getEntityManager

public javax.persistence.EntityManager getEntityManager(javax.persistence.EntityManagerFactory entityManagerFactory,
                                                        java.util.Map properties,
                                                        boolean extended)
                                                 throws java.lang.IllegalStateException
Gets an entity manager instance from the transaction registry, extended regitry or for a transaction scoped entity manager, creates a new one when an exisitng instance is not found.

It is important that a component adds extended scoped entity managers to this registry when the component is entered and removes them when exited. If this registration is not preformed, an IllegalStateException will be thrown when entity manger is fetched.

Parameters:
entityManagerFactory - the entity manager factory from which an entity manager is required
properties - the properties passed to the entity manager factory when an entity manager is created
extended - is the entity manager an extended context
Returns:
the new entity manager
Throws:
java.lang.IllegalStateException - if the entity manger is extended and there is not an existing entity manager instance already registered

addEntityManagers

public void addEntityManagers(java.lang.String deploymentId,
                              java.lang.Object primaryKey,
                              java.util.Map<javax.persistence.EntityManagerFactory,javax.persistence.EntityManager> entityManagers)
                       throws EntityManagerAlreadyRegisteredException
Adds the entity managers for the specified component to the registry. This should be called when the component is entered.

Parameters:
deploymentId - the id of the component
entityManagers - the entity managers to register
Throws:
EntityManagerAlreadyRegisteredException - if an entity manager is already registered with the transaction for one of the supplied entity manager factories; for EJBs this should be caught and rethown as an EJBException

removeEntityManagers

public void removeEntityManagers(java.lang.String deploymentId,
                                 java.lang.Object primaryKey)
Removed the registered entity managers for the specified component.

Parameters:
deploymentId - the id of the component

getInheritedEntityManager

public javax.persistence.EntityManager getInheritedEntityManager(javax.persistence.EntityManagerFactory entityManagerFactory)
Gets an exiting extended entity manager created by a component down the call stack.

Parameters:
entityManagerFactory - the entity manager factory from which an entity manager is needed
Returns:
the existing entity manager or null if one is not found

transactionStarted

public void transactionStarted(java.lang.String deploymentId,
                               java.lang.Object primaryKey)
Notifies the registry that a user transaction has been started or the specified component. When a transaction is started for a component with registered extended entity managers, the entity managers are enrolled in the transaction.

Parameters:
deploymentId - the id of the component

isTransactionActive

public boolean isTransactionActive()
Is a transaction active?

Returns:
true if a transaction is active; false otherwise


Copyright © 1999-2011 The Apache OpenEJB development community. All Rights Reserved.