[hibernate-dev] Temporary session and commit issue with manual flush mode

Łukasz Antoniak lukasz.antoniak at gmail.com
Sat Feb 18 02:27:26 EST 2012


I totally agree with you guys :). However, when I do something like:

if (FlushMode.isManualFlushMode(session.getFlushMode())) {
     Session temporarySession = null;
     try {
         temporarySession = ((Session) session).sessionWithOptions().transactionContext()
                                                                    .connectionReleaseMode(ConnectionReleaseMode.AFTER_TRANSACTION)
                                                                    .openSession();
         executeInSession(temporarySession);
         temporarySession.flush();
     } finally {
         if (temporarySession != null) {
             temporarySession.close();
         }
     }
} else {

I receive an exception:

08:20:40,296 DEBUG SQL:104 - insert into StrTestEntity (id, str) values (null, ?)
08:20:40,359 DEBUG SQL:104 - insert into REVINFO (REV, REVTSTMP) values (null, ?)
08:20:40,375 DEBUG SQL:104 - insert into StrTestEntity_AUD (REVTYPE, str, id, REV) values (?, ?, ?, ?)

javax.persistence.PersistenceException: org.hibernate.ResourceClosedException: This TransactionCoordinator has been closed
	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1360)
	at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1288)
	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1370)
	at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
	at org.hibernate.envers.test.integration.flush.ManualFlush.initData(ManualFlush.java:63)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:63)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.hibernate.testing.junit4.FailureExpectedHandler.evaluate(FailureExpectedHandler.java:59)
	at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.hibernate.testing.junit4.BeforeClassCallbackHandler.evaluate(BeforeClassCallbackHandler.java:43)
	at org.hibernate.testing.junit4.AfterClassCallbackHandler.evaluate(AfterClassCallbackHandler.java:42)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runners.Suite.runChild(Suite.java:128)
	at org.junit.runners.Suite.runChild(Suite.java:24)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:94)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:192)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115)
Caused by: org.hibernate.ResourceClosedException: This TransactionCoordinator has been closed
	at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.getTransaction(TransactionCoordinatorImpl.java:186)
	at org.hibernate.internal.SessionImpl.getTransaction(SessionImpl.java:1303)
	at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1308)
	at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)
	... 40 more

Without explicitly closing the temporary session and autoClose option turned on everything works as expected.

Regards,
Lukasz



More information about the hibernate-dev mailing list