1 22 package org.jboss.ejb3.entity; 23 24 import javax.persistence.EntityManager; 25 import javax.persistence.EntityManagerFactory; 26 import javax.persistence.PersistenceContextType; 27 import javax.persistence.TransactionRequiredException; 28 import javax.transaction.RollbackException ; 29 import javax.transaction.Synchronization ; 30 import javax.transaction.SystemException ; 31 import javax.transaction.Transaction ; 32 import org.jboss.logging.Logger; 33 import org.jboss.tm.TransactionLocal; 34 import org.jboss.tm.TxManager; 35 import org.jboss.tm.TxUtils; 36 import org.jboss.ejb3.ThreadLocalStack; 37 import org.jboss.ejb3.tx.TxUtil; 38 39 import java.util.IdentityHashMap ; 40 import java.util.Map ; 41 42 46 public class ManagedEntityManagerFactory 47 { 48 private static final Logger log = Logger.getLogger(ManagedEntityManagerFactory.class); 49 50 protected EntityManagerFactory entityManagerFactory; 51 protected TransactionLocal session = new TransactionLocal(TxUtil.getTransactionManager()); 52 protected String kernelName; 53 54 public static ThreadLocalStack<Map > nonTxStack = new ThreadLocalStack<Map >(); 55 56 public EntityManager getNonTxEntityManager() 57 { 58 Map map = nonTxStack.get(); 59 EntityManager em = (EntityManager)map.get(this); 60 if (em == null) 61 { 62 em = entityManagerFactory.createEntityManager(); 63 map.put(this, em); 64 } 65 return em; 66 } 67 68 public ManagedEntityManagerFactory(EntityManagerFactory sf, String kernelName) 69 { 70 this.entityManagerFactory = sf; 71 this.kernelName = kernelName; 72 } 73 74 public EntityManagerFactory getEntityManagerFactory() 75 { 76 return entityManagerFactory; 77 } 78 79 public String getKernelName() 80 { 81 return kernelName; 82 } 83 84 public void destroy() 85 { 86 entityManagerFactory.close(); 87 } 88 89 private static class SessionSynchronization implements Synchronization 90 { 91 private EntityManager manager; 92 private Transaction tx; 93 private boolean closeAtTxCompletion; 94 95 public SessionSynchronization(EntityManager session, Transaction tx, boolean close) 96 { 97 this.manager = session; 98 this.tx = tx; 99 closeAtTxCompletion = close; 100 } 101 102 public void beforeCompletion() 103 { 104 122 } 123 124 public void afterCompletion(int status) 125 { 126 if (closeAtTxCompletion) 127 { 128 log.debug("************** closing entity managersession **************"); 129 manager.close(); 130 } 131 } 132 } 133 134 public static ThreadLocal longLivedSession = new ThreadLocal (); 135 136 public TransactionLocal getTransactionSession() 137 { 138 return session; 139 } 140 141 public void registerExtendedWithTransaction(EntityManager pc) 142 { 143 pc.joinTransaction(); 144 session.set(pc); 145 } 146 147 public void verifyInTx() 148 { 149 Transaction tx = session.getTransaction(); 150 if (tx == null || !TxUtils.isActive(tx)) throw new TransactionRequiredException("EntityManager must be access within a transaction"); 151 if (!TxUtils.isActive(tx)) 152 throw new TransactionRequiredException("Transaction must be active to access EntityManager"); 153 } 154 public boolean isInTx() 155 { 156 Transaction tx = session.getTransaction(); 157 if (tx == null || !TxUtils.isActive(tx)) return false; 158 return true; 159 } 160 161 public EntityManager getTransactionScopedEntityManager() 162 { 163 Transaction tx = session.getTransaction(); 164 if (tx == null || !TxUtils.isActive(tx)) return getNonTxEntityManager(); 165 166 EntityManager rtnSession = (EntityManager) session.get(); 167 if (rtnSession == null) 168 { 169 rtnSession = createEntityManager(); 170 try 171 { 172 tx.registerSynchronization(new SessionSynchronization(rtnSession, tx, true)); 173 } 174 catch (RollbackException e) 175 { 176 throw new RuntimeException (e); } 178 catch (SystemException e) 179 { 180 throw new RuntimeException (e); } 182 session.set(rtnSession); 183 rtnSession.joinTransaction(); } 185 return rtnSession; 186 } 187 188 public EntityManager createEntityManager() 189 { 190 return entityManagerFactory.createEntityManager(); 191 } 192 193 194 } 195 | Popular Tags |