@Override public void joinTransaction() { //This checks section 5.6.3.1, throwing an EJBException if there is already a PersistenceContext. if (transactionManager.getResource(persistenceUnit) != null) { throw new EJBException("EntityManager " + transactionManager.getResource(persistenceUnit) + " for persistenceUnit " + persistenceUnit + " already associated with this transaction " + transactionManager.getTransactionKey()); } transactionManager.putResource(persistenceUnit, this); entityManager.joinTransaction(); }
private EntityManager getEntityManager(boolean activeRequired) { TransactionImpl transaction = (TransactionImpl) transactionManager.getTransaction(); if (activeRequired && (transaction == null || transaction.getStatus() != Status.STATUS_ACTIVE)) { throw new TransactionRequiredException("No active transaction"); } if (transaction == null) { return null; } EntityManagerWrapper entityManagerWrapper = (EntityManagerWrapper) transactionManager.getResource(persistenceUnit); if (entityManagerWrapper == null) { EntityManager entityManager = createEntityManager(); entityManagerWrapper = new EntityManagerWrapperTxScoped(entityManager); transactionManager.putResource(persistenceUnit, entityManagerWrapper); try { transaction.registerSynchronization(entityManagerWrapper); } catch (javax.transaction.RollbackException e) { throw (TransactionRequiredException) new TransactionRequiredException("No active transaction").initCause(e); } catch (SystemException e) { throw (TransactionRequiredException) new TransactionRequiredException("No active transaction").initCause(e); } } return entityManagerWrapper.getEntityManager(); }