I have the same problem using hibernate-entitymanager-3.2.1.ga, spring-2.0.4 and WebLogic 9.2MP1.
I use org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean and org.springframework.transaction.jta.WebLogicJtaTra nsactionManager.
I believe Spring should be acting as the container from the JPA perspective, correctly scoping a SharedEntityManager for the duration of the underlying JTA transaction. However a new EntityManager is being used for each transactional method. From a code inspection, this appears to be due to the EntityManagerSynchronization.beforeCompletion() method being invoked for each of these transactional methods even though they are part of an overall JTA transaction. As seen below, this method removes the EntityManager if it thinks it created it (newEntityManager == true).
Code:
public void beforeCompletion() {
if (this.newEntityManager) {
TransactionSynchronizationManager.unbindResource(this.entityManagerFactory);
this.holderActive = false;
this.entityManagerHolder.getEntityManager().close();
}
}
That approach will only work when requesting an EntityManager in a scope that is nested within an existing scope.
Shouldn't the
Code:
TransactionSynchronizationManager.unbindResource(this.entityManagerFactory);
be left until the end of the JTA transaction?
Why is beforeCompletion() being called for each transactional method instead of just once for the JTA transaction?