Results 1 to 3 of 3

Thread: Closing hibernate sessions - Spring 1.2.7 vs 2.5.6

  1. #1
    Join Date
    Dec 2012
    Posts
    2

    Default Closing hibernate sessions - Spring 1.2.7 vs 2.5.6

    Hi,

    I have started maintaining a project which was not initially developed by me. It uses Spring 1.2.7 and I was trying to upgrade the Spring version to 2.5.6.

    Just so you know. I am a beginner in Spring and Hibernate.

    The problem is that when I use Spring 2.5.6 instead of 1.2.7 (everything else remaining the same), I see that jdbc connections are not released when the session is opened the following way and not closed:

    The class extends HibernateDaoSupport
    Code:
    	public List<BookCategory> getAllBookCategories() {
    		Session session = getSessionFactory().openSession();
    		return session.createQuery("from BookCategory bc order by bc.name").list();
    	}
    From what I have read, the session should ideally be closed in the code after it is used or maybe the current session should be used, or the hibernate template. Also, even if I close the session in this method, this is probably not the ideal way of doing this as it looks like the session-per-operation antipattern.

    But this is how it is now and the session does get automatically closed (jdbc connection gets released) after this method call if I use the spring 1.2.7 jar in the project. The moment I replace it with the spring 2.5.6 jar, the session does not get closed after this method call, and after a few such calls, the connection pool gets exhausted and the application hangs. When the database access is through hibernateTemplate, the connections are released normally.

    Here is the sessionFactory configuration:

    Code:
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.MySQLInnoDBDialect
    				</prop>
    				<prop key="hibernate.connection.release_mode">
    					after_transaction
    				</prop>
    			</props>
    		</property>
    		<property name="eventListeners">
    			<map>
    				<entry key="merge">
    					<bean
    						class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
    				</entry>
    			</map>
    		</property>
    		<property name="mappingDirectoryLocations">
    			<list>
    				<value>classpath:/org/bim/library/model</value>
    			</list>
    		</property>
    	</bean>
    I did not see anything that changed from Spring 1.2.7 to 2.5.6 (changelog) which would explain this difference in behavior.

    I am really curious to know the cause of the difference in behavior (even though the code might be incorrect and does not manage the session properly). Is it that spring 1.2.7 was somehow automatically closing the session so the application could get away with not closing the session explicitly and that is no longer the case with Spring 2.5.6.

    Any inputs would be really helpful. Please let me know if you need any more information.

    Thanks for your time.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    You shouldn't use openSession ever in your application. That basically opens a new session outside the scope of spring. There where some major changes between spring 1.2 and 2.5 which changed the way spring uses hibernate. It basically became better. (Spring 1.x used a proxy around hibernate).

    So basically the code is wrong, either use the HibernateTemplate or use getCurrentSession to get a session but NEVER use openSession when you want spring to control your sessions. I suggest the getCurrentSession approach as HibernateTemplate and HibernateDaoSupport should be considered deprecated (as of hibernate 3.0.1) .
    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

  3. #3
    Join Date
    Dec 2012
    Posts
    2

    Default

    Thanks very much Marten for the clarification. I will follow your recommendation regarding using getCurrentSession.

Posting Permissions

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