/** * Associate the extended persistence context with the current JTA transaction (if one is found) * * <p>this method is private to the JPA subsystem */ public void internalAssociateWithJtaTx() { isInTx = TransactionUtil.isInTx(); // ensure that a different XPC (with same name) is not already present in the TX if (isInTx) { // 7.6.3.1 throw EJBException if a different persistence context is already joined to the // transaction (with the same puScopedName). EntityManager existing = TransactionUtil.getTransactionScopedEntityManager(puScopedName); if (existing != null && existing != this) { // should be enough to test if not the same object throw JpaLogger.ROOT_LOGGER.cannotUseExtendedPersistenceTransaction( puScopedName, existing, this); } else if (existing == null) { if (SynchronizationType.SYNCHRONIZED.equals(synchronizationType)) { // JPA 7.9.1 join the transaction if not already done for // SynchronizationType.SYNCHRONIZED. underlyingEntityManager.joinTransaction(); } // associate the entity manager with the current transaction TransactionUtil.putEntityManagerInTransactionRegistry(puScopedName, this); } } }
public synchronized void refCountedClose() { referenceCount--; if (referenceCount == 0) { if (underlyingEntityManager.isOpen()) { underlyingEntityManager.close(); if (isTraceEnabled) { ROOT_LOGGER.tracef("closed extended persistence context (%s)", puScopedName); } } } else if (isTraceEnabled) { ROOT_LOGGER.tracef( "decremented extended persistence context (%s) owner count to %d", puScopedName, referenceCount); } // referenceCount should never be negative, if it is we need to fix the bug that caused it to // decrement too much if (referenceCount < 0) { throw JpaLogger.ROOT_LOGGER.referenceCountedEntityManagerNegativeCount( referenceCount, getScopedPuName()); } }
/** * Catch the application trying to close the container managed entity manager and throw an * IllegalStateException */ @Override public void close() { // An extended entity manager will be closed when the EJB SFSB @remove method is invoked. throw JpaLogger.ROOT_LOGGER.cannotCloseContainerManagedEntityManager(); }