Results 1 to 5 of 5

Thread: reusing sessions across multiple method calls

  1. #1
    Join Date
    Feb 2005
    Posts
    3

    Default reusing sessions across multiple method calls

    I am using Hibernate and Spring in my project. My DAO has several methods like
    getEntity()
    getRelatedEntities()
    etc.

    Since the related entities are loaded lazily, I want to reuse the session across multiple method calls. I want to make sure that getHibernateTemplate() in all the methods in my DAO use the same session instead of creating one for each method call. I have tried using "HibernateTransactionManager" with "LocalSessionFactory"; but whenever I make a call to the DAO, it throws an exception that says "no session is bound or session closed". Do I have to explicitly get a session in the DAO and bind it to the current thread of DAO, so that the session is retained across all method calls in that DAO ?

    Also, the use of SessionFactoryUtils.getSession() method is not very clear to me. When and where can it be used ?

    Responses are much appreciated.

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Your application has a pretty standard use of Hibernate connections. The solution is to keep the HB session in a ThreadLocal (so it has the lifecycle as the calling thread). Spring helps you here with OpenSessionInView Interceptor and OpenSessionInViewFilter from the org.springframework.orm.hibernate.support package.
    Read the javadocs and look at the sample application for a basic use.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Feb 2005
    Posts
    3

    Default

    Well...my application is not a Web application...so, is there any equivalent class for non-web apps ?

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    I can't really tell - I have used the filter only inside web application and I'm not aware of any other similar functionality.
    Try to use the interceptor - it should be possible to use it outside the web application.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Feb 2005
    Posts
    3

    Default

    I am not explicitly opening or closing sessions. I just use getHibernateTemplate(). I think Spring opens and closes session for every method call.

    A typical method in my DAO would look like:
    Code:
    	public void saveEntities&#40;final List<Entity> entities&#41; &#123;
    		getHibernateTemplate&#40;&#41;.execute&#40;new HibernateCallback&#40;&#41; &#123;
    			public Object doInHibernate&#40;Session session&#41; throws net.sf.hibernate.HibernateException, java.sql.SQLException &#123;
    				for &#40;Entity entity &#58; entities&#41; &#123;
    					session.saveOrUpdate&#40;entity&#41;;
    				&#125;				
    				return null;
    			&#125;
    		&#125;&#41;;
     
    	&#125;
    I am interested in reusing the same session object across multiple doInHibernate() calls.

    I also tried using a HibernateTemplate object as a instance variable in my DAO and use the instance variable (and hence the same template) to carry out multiple methods; that didnt seem to help either. A new session is used in every method call.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 PM
  5. Replies: 1
    Last Post: Jul 28th, 2005, 05:08 PM

Posting Permissions

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