title: Hibernate Session is closed in spring transaction after calling method second time
body: <p>First of all I am a beginner in spring.I have created a simple Spring service that has DAO injected and transaction is managed by HibernateTransactionManager of spring, like as below.(And transaction configuration is used using annotations )</p>
  
  <pre><code>@Service(value="daopowered")
  public class UserServiceImplDao implements UserService
      {
      @Inject
      private UserDao userDao;
  
  
      @Override
      @Transactional
      public User autheticate( String userId, String password )
      {
      return userDao.findByIdAndPassword(userId, password);
      } 
  </code></pre>
  
  <p>My transaction configuration is following </p>
  
  <pre><code>&lt;bean id="txMgr"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  &lt;/bean&gt;
  
  &lt;tx:annotation-driven transaction-manager="txMgr" /&gt;
  </code></pre>
  
  <p>Now the problem is when I call authenticate method first time using some controller then it works fine ( does DB operations successfully) but after calling it again second time hibernate session is closed exception is coming ? Please guide me what I am doing it wrong or how to handle this scenario ? Why wont spring opens a new transaction when I call this method second time ?</p>
  
  <p>Exception Trace:</p>
  
  <pre><code>2013-05-22T21:04:18.041+0530 DEBUG [14208212-2] lerExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
  2013-05-22T21:04:18.044+0530 DEBUG [14208212-2] tusExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
  2013-05-22T21:04:18.044+0530 DEBUG [14208212-2] lerExceptionResolver Resolving exception from handler [com.akhi.store.controller.HomeController@5d9d277e]: org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
  2013-05-22T21:04:18.046+0530 DEBUG [14208212-2] et.DispatcherServlet Could not complete request
  org.springframework.orm.hibernate3.HibernateSystemException: Session is closed!; nested exception is org.hibernate.SessionException: Session is closed!
      at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:658)
      at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.convertHibernateAccessException(AbstractSessionFactoryBean.java:245)
      at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.translateExceptionIfPossible(AbstractSessionFactoryBean.java:224)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
      at com.sun.proxy.$Proxy28.findByIdAndPassword(Unknown Source)
  </code></pre>
  
  <p>EDIT: The DAO code</p>
  
  <pre><code>@Repository
  public class UserDaoImpl extends GenericHibernateDAO&lt;User, Long&gt;
                                  implements
                                  UserDao
      {
  
      @Override
      public User findByIdAndPassword( String id, String password )
      {
  
      Criteria crit = getSession().createCriteria(User.class).add(Restrictions.eq("userId",
                                              id)).add(Restrictions.eq("password",
                                                           password)).setMaxResults(1);
      List&lt;?&gt; list = crit.list();
  
      if (list.size() &gt; 0)
          return (User) list.get(0);
      else
          return null;
      }
  </code></pre>
  
  <p>and getSession() implementation is </p>
  
  <pre><code>protected Session getSession() {
      if (session == null) {
          session = sessionFactory.getCurrentSession();
      }
  
      return session;
  }
  </code></pre>
  
  <p>Also the abstract DAO class  has sessionfactory injected</p>
  
  <pre><code>public abstract class GenericHibernateDAO&lt;T, ID extends Serializable&gt;
          implements GenericDAO&lt;T, Long&gt; {
      private Class&lt;T&gt; persistentClass;
  
      protected Session session;
  
      @Autowired
      private SessionFactory sessionFactory;
  </code></pre>