Announcement Announcement Module
Collapse
No announcement yet.
JUnit4, Spring 3.1, Hibernate 3/4 = session is closed between tests. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit4, Spring 3.1, Hibernate 3/4 = session is closed between tests.

    Hi,

    I have been researching this problem for ages with no solution, and perhaps there is something I dont understand, or am missing.

    My app is using spring 3.1 and Hibernate 4. I have created an integration test with JUnit 4 as follows (just as an example) and running in STS:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:mobileu/system-test-config.xml")
    @TransactionConfiguration
    @Transactional
    Code:
    public class MobileUTests {
    	
    	@Autowired
    	private ApplicationDefinitionService applicationDefinitionService;
    	
    	@Before
    	public void setUp () {
    	}
    	
    	@Test
    	/**
    	 * Test that a new ApplicationDefinition object can be created and saved to the database.
    	 */
    	public void testCreateApplicationDefinition1() {
    		String name = "Application Name";
    		String description = "Application Description";
    		ApplicationDefinition applicationDefinition = new ApplicationDefinition();
    		applicationDefinition.setName(name);
    		applicationDefinition.setDescription(description);
    		applicationDefinitionService.saveApplicationDefinition(applicationDefinition);
    	}
    	
    	@Test
    	/**
    	 * Test that a new ApplicationDefinition object can be created and saved to the database.
    	 */
    	public void testCreateApplicationDefinition2() {
    		String name = "Application Name";
    		String description = "Application Description";
    		ApplicationDefinition applicationDefinition = new ApplicationDefinition();
    		applicationDefinition.setName(name);
    		applicationDefinition.setDescription(description);
    		applicationDefinitionService.saveApplicationDefinition(applicationDefinition);
    	}
    When running the test, the testCreateApplicationDefinition1 method runs fine, but the testCreateApplicationDefinition2 method fails with a "org.hibernate.SessionException: Session is closed!".

    If I annotate with @DirtiesContext, then the context is re-created between tests and it works fine. However, I don't want to re-load the context between tests.

    What am I missing, why is the hibernate session closing between tests? How am I supposed to run integration tests with Spring 3.1 and Hibernate ? I have tried both Hibernate 3 and 4.

    Config below:
    Code:
    <context:component-scan base-package="mobileu" />		
    <context:annotation-config />
    <tx:annotation-driven/>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="packagesToScan">
    			<list>
    				<value>mobileu/domain</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<value>
    				hibernate.format_sql=true
    				hibernate.show_sql=true
    				hibernate.hbm2ddl.auto=update
    				hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    			</value>
    		</property>
    	</bean>
    	
    	<!-- A transaction manager for working with Hibernate SessionFactories -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	
    	<context:property-placeholder location="classpath:mobileu/systemtest.properties"/>
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="${db.driverClassName}"/>
            <property name="url" value="${db.url}"/>
            <property name="username" value="${db.userName}"/>
            <property name="password" value="${db.password}"/>
            <property name="maxActive" value="100"/>
            <property name="maxIdle" value="30"/>
            <property name="maxWait" value="1000"/>
            <property name="defaultAutoCommit" value="true"/>
    	</bean>

  • #2
    Not to worry, I found the problem. It was nothing to do with Spring, Hibernate and JUnit, but a problem in my HibernateRepository class where it was only checking if the session was null and then calling "session = sessionFactory.getCurrentSession();" and it wasnt checking for "session.isOpen()".

    Comment


    • #3
      Ehrm... The description of the code makes me shudder... I really hope you aren't storing the session in a instance scoped variable... NEVER do that... You might end up with several users using the same hibernate session you don't want that ...

      Comment


      • #4
        Thanks for taking the time to give the heads up.

        I was originally doing that (was a quick class draft in order to structure my integration test and didn't give it much thought).

        Now I have a HibernateRepository class which will be extended by all my Hibernate Repositores, which looks as follows:

        Code:
        public class HibernateRepository {
        	private SessionFactory sessionFactory;
        	
        	public HibernateRepository () {}
        
        	/**
        	 * @param sessionFactory the sessionFactory to set
        	 */
        	@Autowired
        	public void setSessionFactory(SessionFactory sessionFactory) {
        		this.sessionFactory = sessionFactory;
        	}
        	
        	protected Session getSession () {
        		return sessionFactory.getCurrentSession();
        	}
        
        }

        Comment

        Working...
        X