" Sice Hibernate ORM > 5.1 there seems to be a problem with ForeignGenerator when using Hibernate with a JTA transaction manager in conjuncton with Spring 4.3+.
When \r\n\r\nWhen not explicitly flusing the entity manager within the transaction, the EntityManager and the Hibernate session is logically closed before the transaction is commited and hence before the hibernate session is flushed. This is a spring handling within org.springframework.transaction.support.ResourceHolderSynchronization.beforeCompletion()
The \r\n\r\nThe problem is, that the ForeignGenerator uses Session.contains performing a close-check after the session has been closed.
\r\n\r\nThe attached test case contains two tests org.hibernate.bugs.JPAwithJTAUnitTestCase:\r\n* *addRelationImplicitFlush*: a failing test relying on implict flush on commit\r\n* *addRelationExplicitFlush*: a working test when explicitly flushing the entity manager within the transaction\r\n\r\n {code:java} org \r\norg .springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is bitronix.tm.internal.BitronixRollbackException: RuntimeException thrown during beforeCompletion cycle caused transaction rollback at \r\n\tat org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1024) at \r\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:765) at \r\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:734) at \r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:518) at \r\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) at \r\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at \r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at \r\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) at \r\n\tat com.sun.proxy.$Proxy60.addContract(Unknown Source) at \r\n\tat org.hibernate.bugs.JPAwithJTAUnitTestCase.addRelationImplicitFlush(JPAwithJTAUnitTestCase.java:62) at \r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at \r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at \r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at \r\n\tat java.lang.reflect.Method.invoke(Method.java:498) at \r\n\tat org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at \r\n\tat org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at \r\n\tat org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at \r\n\tat org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at \r\n\tat org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) at \r\n\tat org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) at \r\n\tat org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) at \r\n\tat org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at \r\n\tat org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) at \r\n\tat org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at \r\n\tat org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at \r\n\tat org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at \r\n\tat org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at \r\n\tat org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at \r\n\tat org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at \r\n\tat org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at \r\n\tat org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at \r\n\tat org.junit.runners.ParentRunner.run(ParentRunner.java:363) at \r\n\tat org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at \r\n\tat org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at \r\n\tat org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at \r\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at \r\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at \r\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at \r\n\tat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused \r\nCaused by: bitronix.tm.internal.BitronixRollbackException: RuntimeException thrown during beforeCompletion cycle caused transaction rollback at \r\n\tat bitronix.tm.BitronixTransaction.commit(BitronixTransaction.java:241) at \r\n\tat bitronix.tm.BitronixTransactionManager.commit(BitronixTransactionManager.java:143) at \r\n\tat org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1021) \r\n\t ... 38 more Caused \r\nCaused by: java.lang.IllegalStateException: Session/EntityManager is closed at \r\n\tat org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:357) at \r\n\tat org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:138) at \r\n\tat org.hibernate.internal.SessionImpl.contains(SessionImpl.java:2110) at \r\n\tat org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:123) at \r\n\tat org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:479) at \r\n\tat org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:97) at \r\n\tat org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123) at \r\n\tat org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192) at \r\n\tat org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135) at \r\n\tat org.hibernate.internal.SessionImpl.firePersistOnFlush(SessionImpl.java:860) at \r\n\tat org.hibernate.internal.SessionImpl.persistOnFlush(SessionImpl.java:853) at \r\n\tat org.hibernate.engine.spi.CascadingActions$8.cascade(CascadingActions.java:341) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:471) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:396) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:197) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:504) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:436) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:399) at \r\n\tat org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:197) at \r\n\tat org.hibernate.engine.internal.Cascade.cascade(Cascade.java:130) at \r\n\tat org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:159) at \r\n\tat org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:150) at \r\n\tat org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:83) at \r\n\tat org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38) at \r\n\tat org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454) at \r\n\tat org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:511) at \r\n\tat org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3278) at \r\n\tat org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2474) at \r\n\tat org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473) at \r\n\tat org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.beforeCompletion(JtaTransactionCoordinatorImpl.java:352) at \r\n\tat org.hibernate.resource.transaction.backend.jta.internal.synchronization.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:47) at \r\n\tat org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:37) at \r\n\tat bitronix.tm.BitronixTransaction.fireBeforeCompletionEvent(BitronixTransaction.java:532) at \r\n\tat bitronix.tm.BitronixTransaction.commit(BitronixTransaction.java:235) \r\n\t ... 40 more \r\n {code} \r\n" |
|