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.
| Method from org.apache.openejb.persistence.JtaEntityManagerRegistry Detail: |
public void addEntityManagers(String deploymentId,
Object primaryKey,
Map entityManagers) throws EntityManagerAlreadyRegisteredException {
extendedRegistry.get().addEntityManagers(new InstanceId(deploymentId, primaryKey), entityManagers);
}
Adds the entity managers for the specified component to the registry. This should be called when the component
is entered. |
public EntityManager getEntityManager(EntityManagerFactory entityManagerFactory,
Map properties,
boolean extended) throws IllegalStateException {
if (entityManagerFactory == null) throw new NullPointerException("entityManagerFactory is null");
EntityManagerTxKey txKey = new EntityManagerTxKey(entityManagerFactory);
boolean transactionActive = isTransactionActive();
// if we have an active transaction, check the tx registry
if (transactionActive) {
EntityManager entityManager = (EntityManager) transactionRegistry.getResource(txKey);
if (entityManager != null) {
return entityManager;
}
}
// if extended context, there must be an entity manager already registered with the tx
if (extended) {
EntityManager entityManager = getInheritedEntityManager(entityManagerFactory);
if (entityManager == null) {
throw new IllegalStateException("InternalError: an entity manager should already be registered for this entended persistence unit");
}
// if transaction is active, we need to register the entity manager with the transaction manager
if (transactionActive) {
entityManager.joinTransaction();
transactionRegistry.putResource(txKey, entityManager);
}
return entityManager;
} else {
// create a new entity manager
EntityManager entityManager;
if (properties != null) {
entityManager = entityManagerFactory.createEntityManager(properties);
} else {
entityManager = entityManagerFactory.createEntityManager();
}
// if we are in a transaction associate the entity manager with the transaction; otherwise it is
// expected the caller will close this entity manager after use
if (transactionActive) {
transactionRegistry.registerInterposedSynchronization(new CloseEntityManager(entityManager));
transactionRegistry.putResource(txKey, entityManager);
}
return entityManager;
}
}
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. |
public EntityManager getInheritedEntityManager(EntityManagerFactory entityManagerFactory) {
return extendedRegistry.get().getInheritedEntityManager(entityManagerFactory);
}
Gets an exiting extended entity manager created by a component down the call stack. |
public boolean isTransactionActive() {
int txStatus = transactionRegistry.getTransactionStatus();
boolean transactionActive = txStatus == Status.STATUS_ACTIVE || txStatus == Status.STATUS_MARKED_ROLLBACK;
return transactionActive;
}
|
public void removeEntityManagers(String deploymentId,
Object primaryKey) {
extendedRegistry.get().removeEntityManagers(new InstanceId(deploymentId, primaryKey));
}
Removed the registered entity managers for the specified component. |
public void transactionStarted(String deploymentId,
Object primaryKey) {
extendedRegistry.get().transactionStarted(new InstanceId(deploymentId, 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. |