Results 1 to 5 of 5

Thread: Should I close the session per operation?

  1. #1
    Join Date
    May 2005
    Location
    China
    Posts
    8

    Default Should I close the session per operation?

    Code:
    package org.nirvana.jswiki.dao.impl;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.nirvana.jswiki.dao.BaseDAO;
    
    public class BaseDAOImpl implements BaseDAO {
    
    	protected SessionFactory sessionFactory;
    	
    	public void create(Object o) {
    		Session session = sessionFactory.getCurrentSession();
    		session.save(o);
    	}
    
    	public void save(Object o) {
    		Session session = sessionFactory.getCurrentSession();
    		session.save(o);
    	}
    
    	public void update(Object o) {
    		Session session = sessionFactory.getCurrentSession();
    		session.update(o);
    	}
    
    	public void saveOrUpdate(Object o) {
    		Session session = sessionFactory.getCurrentSession();
    		session.saveOrUpdate(o);
    	}
    
    	public SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    
    	
    }
    I wrote my BaseDAO, and if I closed the session in each method, and following Test would failure.
    Code:
    		UserDAO dao = (UserDAO) ctx.getBean("userDAOProxy");
    		User u = dao.retriveByName("Nicholas");
    		
    		u = new User();
    		u.setName("Helen");
    		u.setEmail("Helen@hotmail.com");
    		dao.save(u);
    		
    		assertEquals("Helen", u.getName());
    If I did not close the session, this Test would be OK.
    Does Spring manage per sesson out-of-box?[/code]

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    We recommend using the HibernateTemplate provided by Spring.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    May 2005
    Location
    China
    Posts
    8

    Default

    How about implementating DAOs based on plain on Hibernate3 API?
    It said in Spring-Refernce chatper 11.2.5, I found that feature amazing.

  4. #4
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    SessionFactory.getCurrentSession() can indeed be used to code DAOs against the plain Hibernate3 API. The main advantage of this is that you only rely on standard Hibernate API and don't have Spring dependencies in your DAOs. The main disadvantage is that such DAOs will throw plain HibernateException, not Spring's DataAccessException - which doesn't allow callers to handle exceptions in a generic manner. If the latter is important to you, use Spring's HibernateTemplate - which we generally recommend. See the ORM chapter in our reference manual for details.

    Note that such a getCurrentSession() call does not open a Session for each call: It just accesses the current transactional Session, whose lifecycle is managed by the current transaction. Hence, you don't need to close it: just get a reference to it and access it, leaving the lifecycle to the transaction manager (either Spring's HibernateTransactionManager or JTA).

    Juergen

  5. #5
    Join Date
    May 2005
    Location
    China
    Posts
    8

    Default

    Thank you for your explaination.
    Now I using OpenSessionInViewInterceptor for per request and everything is OK.

Similar Threads

  1. OpenSessionInView and portlet support
    By garpinc2 in forum Web Flow
    Replies: 31
    Last Post: Apr 9th, 2010, 11:12 AM
  2. Replies: 2
    Last Post: Aug 31st, 2005, 12:37 PM
  3. Replies: 1
    Last Post: Mar 12th, 2005, 04:33 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Replies: 9
    Last Post: Sep 25th, 2004, 12:35 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
  •