1 25 26 package org.objectweb.easybeans.persistence; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.persistence.EntityManager; 32 import javax.persistence.EntityManagerFactory; 33 import javax.transaction.RollbackException ; 34 import javax.transaction.Status ; 35 import javax.transaction.SystemException ; 36 import javax.transaction.Transaction ; 37 38 import org.objectweb.easybeans.transaction.JTransactionManager; 39 40 45 public class TxEntityManagerHandler { 46 47 50 private EntityManagerFactory entityManagerFactory = null; 51 52 55 private Map <Transaction , EntityManager> entityManagers = null; 56 57 60 private ThreadLocal <EntityManager> previousEntityManager = new ThreadLocal <EntityManager>(); 61 62 67 private ThreadLocal <EntityManager> threadEntityManager = new ThreadLocal <EntityManager>(); 68 69 75 public TxEntityManagerHandler(final EntityManagerFactory entityManagerFactory) { 76 this.entityManagerFactory = entityManagerFactory; 77 this.entityManagers = new HashMap <Transaction , EntityManager>(); 78 } 79 80 85 public synchronized EntityManager getCurrent() { 86 EntityManager current = null; 87 88 Transaction currentTx = null; 90 try { 91 currentTx = JTransactionManager.getTransactionManager().getTransaction(); 92 } catch (SystemException e) { 93 throw new IllegalStateException ("Cannot get current transaction", e); 94 } 95 96 int statusTx = Status.STATUS_UNKNOWN; 98 if (currentTx != null) { 99 try { 100 statusTx = currentTx.getStatus(); 101 } catch (SystemException e) { 102 throw new IllegalStateException ("Cannot get the status on the current transaction", e); 103 } 104 } 105 111 if (currentTx == null || !(statusTx == Status.STATUS_ACTIVE)) { 112 return threadEntityManager.get(); 113 } 114 115 current = entityManagers.get(currentTx); 116 121 if (current == null) { 122 128 current = buildNewTxEntityManager(currentTx); 129 } 130 131 return current; 132 } 133 134 139 private EntityManager buildNewTxEntityManager(final Transaction tx) { 140 EntityManager entityManager = entityManagerFactory.createEntityManager(); 141 142 147 try { 148 tx.registerSynchronization(new TxEntityManagerLifeCycle(entityManager, tx, this)); 149 } catch (IllegalStateException e) { 150 throw new IllegalStateException ("Cannot register Entity manager lifecycle", e); 151 } catch (RollbackException e) { 152 throw new IllegalStateException ("Cannot register Entity manager lifecycle", e); 153 } catch (SystemException e) { 154 throw new IllegalStateException ("Cannot register Entity manager lifecycle", e); 155 } 156 157 entityManagers.put(tx, entityManager); 159 160 return entityManager; 161 } 162 163 167 private EntityManager buildNoTxEntityManager() { 168 return entityManagerFactory.createEntityManager(); 169 } 170 171 175 public void release(final Transaction tx) { 176 entityManagers.remove(tx); 177 } 178 179 182 public void addCurrent() { 183 previousEntityManager.set(threadEntityManager.get()); 184 threadEntityManager.set(buildNoTxEntityManager()); 185 } 186 187 191 public void closeCurrentAndReturnToPrevious() { 192 EntityManager current = threadEntityManager.get(); 193 threadEntityManager.set(previousEntityManager.get()); 195 196 if (current != null) { 198 current.close(); 199 } 200 201 } 202 } 203 | Popular Tags |