title: Spring MVC - No Hibernate Session bound to thread
body: <p>I have problem with session in hibernate:</p>
  
  <pre><code>INFO: Starting ProtocolHandler ["http-bio-8080"]
  2013-09-10 16:23:11 org.apache.catalina.startup.Catalina start
   INFO: Server startup in 4000 ms
  2013-09-10 16:23:13 org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [appServlet] in context with path [/portal] threw          exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here] with root cause
  org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
      at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
      at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
      at com.myportal.portal.model.HibernateDao.getSessionFactory(HibernateDao.java:31)
      at com.myportal.portal.model.HibernateDao.testowa(HibernateDao.java:46)
      at com.myportal.portal.controllers.HomeController.home(HomeController.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  </code></pre>
  
  <p>I've read something about this problem but the solutions don't work in my case.</p>
  
  <p>My Dao class:</p>
  
  <pre><code>package com.myportal.portal.model;
  import org.hibernate.classic.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Propagation;
  import org.springframework.transaction.annotation.Transactional;
  import com.myportal.portal.entity.User;
  
  @Repository
  @Transactional(propagation=Propagation.REQUIRED)
  public class HibernateDao {
      @Autowired
      private SessionFactory sessionFactory;
  
  
      public HibernateDao()
      {
  
      }
      public HibernateDao(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
      private Session getSessionFactory()
      {
          return sessionFactory.getCurrentSession();
      }
  
      private void setSessionFactory(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      public void testowa()
      {
          User u = new User();
          //SessionFactory sf = getSessionFactory();
          //Session s = sf.openSession().beginTransaction()
  
          // problem with this
          Session session = getSessionFactory();
          //session.save(u);
  
      }
  }
  </code></pre>
  
  <p>root-context.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  
  
  &lt;context:annotation-config/&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost/hibernate1"/&gt;
      &lt;property name="username" value="root2"/&gt;
      &lt;property name="password" value=""/&gt;
      &lt;property name="initialSize" value="5"/&gt;
      &lt;property name="maxActive" value="10"/&gt;
  &lt;/bean&gt;    
  
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
     &lt;property name="dataSource" ref="dataSource"/&gt;
     &lt;property name="packagesToScan" value="com.spoleczniak.projekt.model"/&gt;
     &lt;property name="hibernateProperties"&gt;
         &lt;props&gt;
             &lt;prop key="dialect"&gt;org.hibernate.dialect.MySQLInnoDBDialect&lt;/prop&gt;
             &lt;prop key="show_sql"&gt;true&lt;/prop&gt;
         &lt;/props&gt;
     &lt;/property&gt;
  &lt;/bean&gt;
  
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
       &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>servlet-context.xml</p>
  
  <pre><code>    &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
  
      &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
  &lt;annotation-driven /&gt;
      &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.myportal.portal" /&gt;
  
      &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt;
      &lt;resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt;
      &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;beans:property name="suffix" value=".jsp" /&gt;
      &lt;/beans:bean&gt;
  </code></pre>
  
  <p>I use it like this:</p>
  
  <pre><code>@Controller
  public class RegisterController {
  
      @Autowired
      private HibernateDao hibernateDao;
  
      @RequestMapping(value="/register")
      public String registerForm()
      {
          hibernateDao.testowa();
          return "register";
      }
  }
  </code></pre>
  
  <p>How can I fix it?</p>
  
  <p>I change DAO and create UserService, but error still appears :</p>
  
  <p>DAO:</p>
  
  <pre><code>@Repository
  public class HibernateDao {
      @Autowired
      private SessionFactory sessionFactory;
  
  
      public HibernateDao()
      {
  
      }
      public HibernateDao(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
      private Session getSessionFactory()
      {
          return sessionFactory.getCurrentSession();
      }
  
      private void setSessionFactory(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      public void testowa()
      {
          User u = new User();
          //SessionFactory sf = getSessionFactory();
          //Session s = sf.openSession().beginTransaction()
  
          // problem with this
          Session session = getSessionFactory();
          //session.save(u);
  
      }
  } 
  </code></pre>
  
  <p>UserService:</p>
  
  <pre><code>@Service
  public class UserService {
  
      @Autowired
      private HibernateDao hibernateDao;
  
  
      @Transactional
      public void addContact() 
      {
          hibernateDao.testowa(); 
      }
  }
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@Controller
  public class RegisterController {
  
      @Autowired
      private UserService userService;
  
      @RequestMapping(value="/register")
      public String registerForm()
      {
          userService.addContact();
          return "register";
      }
  }
  </code></pre>