1 19 package org.apache.cayenne.jpa; 20 21 import java.util.Map ; 22 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingException ; 25 import javax.persistence.EntityManager; 26 import javax.persistence.spi.PersistenceUnitInfo; 27 import javax.transaction.Status ; 28 import javax.transaction.TransactionSynchronizationRegistry ; 29 30 import org.apache.cayenne.access.DataDomain; 31 32 39 public class JtaEntityManagerFactory extends ResourceLocalEntityManagerFactory { 40 41 static final String TX_SYNC_REGISTRY_KEY = "java:comp/TransactionSynchronizationRegistry"; 42 43 protected TransactionSynchronizationRegistry transactionRegistry; 44 45 48 JtaEntityManagerFactory(PersistenceUnitInfo unitInfo) { 49 super(unitInfo); 50 } 51 52 public JtaEntityManagerFactory(Provider provider, DataDomain domain, 53 PersistenceUnitInfo unitInfo) { 54 super(provider, domain, unitInfo); 55 } 56 57 61 protected TransactionSynchronizationRegistry getTransactionRegistry() { 62 if (transactionRegistry == null) { 63 InitialContext jndiContext; 64 try { 65 jndiContext = new InitialContext (); 66 } 67 catch (NamingException e) { 68 throw new JpaProviderException("Error creating JNDI context", e); 69 } 70 71 try { 72 transactionRegistry = (TransactionSynchronizationRegistry ) jndiContext 73 .lookup(TX_SYNC_REGISTRY_KEY); 74 } 75 catch (NamingException e) { 76 throw new JpaProviderException("Failed to look up " 77 + TX_SYNC_REGISTRY_KEY, e); 78 } 79 } 80 81 return transactionRegistry; 82 } 83 84 87 protected boolean isActiveTransaction() { 88 int txStatus = getTransactionRegistry().getTransactionStatus(); 89 return txStatus == Status.STATUS_ACTIVE 90 || txStatus == Status.STATUS_MARKED_ROLLBACK; 91 } 92 93 @Override 94 public EntityManager createEntityManager(Map map) { 95 checkClosed(); 96 CayenneEntityManager em = new JtaEntityManager(createObjectContext(), this); 97 em = new TypeCheckingEntityManager(em); 98 99 if (isActiveTransaction()) { 100 em.joinTransaction(); 101 } 102 103 return em; 104 } 105 } 106 | Popular Tags |