Results 1 to 4 of 4

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

  1. #1
    Join Date
    Feb 2012
    Posts
    16

    Default 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. #2
    Join Date
    Feb 2012
    Posts
    16

    Default

    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()".

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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 ...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Feb 2012
    Posts
    16

    Default

    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();
    	}
    
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •