| Calling getTransaction() on an already closed (app managed) entity manager shouldn't throw IllegalStateException, instead we should return null. Looking at the 5.1 org.hibernate.jpa.spi.AbstractEntityManagerImpl, we simply had the following, which I think would return null if called on a closed app managed entity manager:
public EntityTransaction getTransaction() {
if ( transactionType == PersistenceUnitTransactionType.JTA ) {
throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" );
}
return tx;
}
From EntityManager#close javadoc:
Close an application-managed entity manager. After the close method has been invoked, all methods on the EntityManager instance and any Query, TypedQuery, and StoredProcedureQuery objects obtained from it will throw the IllegalStateException except for getProperties, getTransaction, and isOpen (which will return false). If this method is called when the entity manager is joined to an active transaction, the persistence context remains managed until the transaction completes.
The exception call stack for the thrown ISE is:
java.lang.IllegalStateException: org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@1038f80a is closed [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.errorIfClosed(AbstractLogicalConnectionImplementor.java:37) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.getPhysicalJdbcTransaction(AbstractLogicalConnectionImplementor.java:31) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.getResourceLocalTransaction(JdbcCoordinatorImpl.java:489) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.getTransactionDriverControl(JdbcResourceLocalTransactionCoordinatorImpl.java:93) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.engine.transaction.internal.TransactionImpl.<init>(TransactionImpl.java:45) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.internal.AbstractSharedSessionContract.accessTransaction(AbstractSharedSessionContract.java:399) [javatest.batch] 13:48:09,281 INFO [stdout] (Thread-127) at org.hibernate.internal.AbstractSharedSessionContract.getTransaction(AbstractSharedSessionContract.java:390) [javatest.batch] 13:48:09,282 INFO [stdout] (Thread-127) at org.hibernate.internal.AbstractSessionImpl.getTransaction(AbstractSessionImpl.java:23)
|