Announcement Announcement Module
No announcement yet.
CannotCreateTransactionException non-forked integration testing "Session is closed!" Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • CannotCreateTransactionException non-forked integration testing "Session is closed!"

    We are converting to using spring-3.1.0.RC2 and hibernate-4.0.0.CR7

    We have several hundred integration tests. When they are run in a forked mode (<forkMode>always</forkMode>, which takes much longer) all tests pass. When they are run in non-forked mode (much quicker) they begin to fail after the 28th integration test class is run.

    Each of the failing integration tests passes independently.

    Here is a stack trace (test result) from the first of the failures:

    Test set:
    Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.036 sec <<< FAILURE!
    testGetDiseaseByClinicalCode(  Time elapsed: 0.018 sec  <<< ERROR!
    org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.SessionException: Session is closed!
    	at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener$TransactionContext.startTransaction(
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.startNewTransaction(
    	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(
    	at org.springframework.test.context.TestContextManager.beforeTestMethod(
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(
    	at org.junit.runners.ParentRunner$
    	at org.junit.runners.ParentRunner$1.schedule(
    	at org.junit.runners.ParentRunner.runChildren(
    	at org.junit.runners.ParentRunner.access$000(
    	at org.junit.runners.ParentRunner$2.evaluate(
    	at org.junit.internal.runners.statements.RunBefores.evaluate(
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(
    	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(
    	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    	at java.lang.reflect.Method.invoke(
    	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(
    	at org.apache.maven.surefire.booter.SurefireBooter.main(
    Caused by: org.hibernate.SessionException: Session is closed!
    	at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(
    	at org.hibernate.internal.SessionImpl.connection(
    	at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(
    	... 30 more

    Q: Why would forking of the integration tests pass (a new jvm for each test) versus non-forked failing?

    Q: Any suggestions on how best to track down the issue?

    Thanks in advance. :-)
    Last edited by javapda; Dec 15th, 2011, 06:13 AM.

  • #2
    Using spring's transaction management vs. manual management

    The test methods of the integration tests make use of the @Transactional annotation. Using @Transactional seems to work fine within individual integration tests.

    If, we use a manual approach to the behavior is altered in that the underlying SessionHolder object seems to be cleared of its session upon completion of most persist operations.

        private Session session;
        protected void startTransactionSynchronization() {
            session = getCurrentSession();
                    new SessionHolder(session));
        protected void stopTransactionSynchronization() {
            if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
              if (session == null) {
        private Session getCurrentSession() {
            if(session==null) {
                session = sessionFactory.openSession();
            } else if (!session.isOpen()) {
                session = sessionFactory.openSession();
            return session;


    • #3
      Root cause

      The root cause seems to be related to intermixing the spring Transaction Manager (affected by the @Transactional annotation) with tests that run using a manually choreographed transaction (see startTransaction() above).

      Here's how it was tracked down. We run automated tests from jenkins (hudson) via a maven test goal. The actually tests run (and fork mode) are controlled by settings in the configuration setting of the maven-surefire-plugin. (see plugin stanza from pom.xml below)

      							<argLine>-Xms512m -Xmx2024m -XX:PermSize=64M -XX:MaxPermSize=256M
      Notice the <include> tags. Here you can use ant-like patterns to constrain which tests are run. We began by narrowing the population of integration tests run.
      It was mentioned earlier that the test failures occurred later in the test run but individual tests ran successfully. Soon it was discovered that some of the tests run prior to test failure included manual transaction handling by binding the sessionFactory to a session holder object in the TransactionSynchronizationManager ( onSynchronizationManager). As the javadoc states "Central helper that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code." We were obviously not adhering to this advice.

      It seems that if you really need to do some discrete hibernate session activity you could simply work directly with Session objects explicitly generated from a session factory, then perform the operations on this session, without the express involvement of the spring transaction manager.