org.objectweb.easybeans.persistence
Class TxEntityManagerHandler

java.lang.Object
  extended by org.objectweb.easybeans.persistence.TxEntityManagerHandler

public class TxEntityManagerHandler
extends java.lang.Object

This class is used to handle different entity manager for Transaction Scoped Persistence Context. It associates one EntityManager to a given transaction

Author:
Florent Benoit

Field Summary
private  EntityManagerFactory entityManagerFactory
          EntityManager factory that will create entity manager.
private  java.util.Map<javax.transaction.Transaction,EntityManager> entityManagers
          Map between transactions and the entity manager.
private  java.lang.ThreadLocal<EntityManager> previousEntityManager
          Previous entity manager (must be kept).
private  java.lang.ThreadLocal<EntityManager> threadEntityManager
          Entity Manager used for a single method call when there is no TX.
 
Constructor Summary
TxEntityManagerHandler(EntityManagerFactory entityManagerFactory)
          Build a new handler of these entity managers.
 
Method Summary
 void addCurrent()
          Sets the current entity manager (used when to transaction is active).
private  EntityManager buildNewTxEntityManager(javax.transaction.Transaction tx)
          Builds a new Entity manager for the given transaction.
private  EntityManager buildNoTxEntityManager()
          Builds a new Entity manager used when there is no transaction.
 void closeCurrentAndReturnToPrevious()
          Sets back to the previous entity manager and close the current entity manager.
 EntityManager getCurrent()
          Gets the current entity manager (or create one) for the given transaction if there is one.
 void release(javax.transaction.Transaction tx)
          Release the entity manager associated to the given tx.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

entityManagerFactory

private EntityManagerFactory entityManagerFactory
EntityManager factory that will create entity manager.


entityManagers

private java.util.Map<javax.transaction.Transaction,EntityManager> entityManagers
Map between transactions and the entity manager.


previousEntityManager

private java.lang.ThreadLocal<EntityManager> previousEntityManager
Previous entity manager (must be kept). Used when there is no TX.


threadEntityManager

private java.lang.ThreadLocal<EntityManager> threadEntityManager
Entity Manager used for a single method call when there is no TX. An interceptor will request to create a new entity manager and will close it at the end of the method.

Constructor Detail

TxEntityManagerHandler

public TxEntityManagerHandler(EntityManagerFactory entityManagerFactory)
Build a new handler of these entity managers.

Parameters:
entityManagerFactory - Factory used to create the entity manager (if no entity manager is already associated to the current transaction)
Method Detail

getCurrent

public EntityManager getCurrent()
Gets the current entity manager (or create one) for the given transaction if there is one.

Returns:
an entitymanager

buildNewTxEntityManager

private EntityManager buildNewTxEntityManager(javax.transaction.Transaction tx)
Builds a new Entity manager for the given transaction.

Parameters:
tx - transaction on which associate the entity manager
Returns:
built entity manager.

buildNoTxEntityManager

private EntityManager buildNoTxEntityManager()
Builds a new Entity manager used when there is no transaction.

Returns:
built entity manager.

release

public void release(javax.transaction.Transaction tx)
Release the entity manager associated to the given tx.

Parameters:
tx - tx to removed

addCurrent

public void addCurrent()
Sets the current entity manager (used when to transaction is active).


closeCurrentAndReturnToPrevious

public void closeCurrentAndReturnToPrevious()
Sets back to the previous entity manager and close the current entity manager.