title: Spring-Hibernate - No Session found for current thread
body: <p>I have a java stuts2 web application using spring and hibernate.</p>
  
  <p>Im getting <code>org.hibernate.HibernateException: No Session found for current thread</code>.</p>
  
  <p><strong>SpringBean.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
      &lt;context:component-scan base-package="org.rohith" /&gt;
      &lt;context:annotation-config /&gt;
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="url" value="jdbc:mysql://localhost:3306/company" /&gt;
          &lt;property name="username" value="root" /&gt;
          &lt;property name="password" value="password" /&gt;
      &lt;/bean&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
  
          &lt;property name="dataSource"&gt;
              &lt;ref bean="dataSource" /&gt;
          &lt;/property&gt;
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
  
          &lt;property name="mappingResources"&gt;
              &lt;list&gt;
                  &lt;value&gt;hibernate.cfg.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
      &lt;/bean&gt;
      &lt;!-- &lt;tx:annotation-driven/&gt; --&gt;
      &lt;bean id = "transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name = "sessionFactory" ref = "sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="customerDaoImpl" class="org.rohith.dao.impl.CustomerDaoImpl"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="customerServiceImpl" class="org.rohith.service.impl.CustomerServiceImpl"&gt;
          &lt;property name="customerDaoImpl" ref="customerDaoImpl"/&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>hibernate.cfg.xml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
     &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;  
          &lt;!-- Names the annotated entity class --&gt;
          &lt;mapping class="org.rohith.model.Customer"/&gt;    
      &lt;/session-factory&gt;  
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p><strong>CustomerServiceImpl.java</strong></p>
  
  <pre><code>package org.rohith.service.impl;
  
  import org.rohith.dao.impl.CustomerDaoImpl;
  import org.rohith.model.Customer;
  import org.rohith.service.CustomerService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  @Service
  public class CustomerServiceImpl implements CustomerService {
  
      @Autowired
      private CustomerDaoImpl customerDaoImpl;
  
      @Override
      public void saveCustomer(Customer customer) {
          customerDaoImpl.saveCustomer(customer);
      }
      public CustomerDaoImpl getCustomerDaoImpl() {
          return customerDaoImpl;
      }
      public void setCustomerDaoImpl(CustomerDaoImpl customerDaoImpl) {
          this.customerDaoImpl = customerDaoImpl;
      }
  
  }
  </code></pre>
  
  <p><strong>CustomerDaoImpl.java</strong></p>
  
  <pre><code>package org.rohith.dao.impl;
  
  import org.hibernate.HibernateException;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.rohith.dao.CustomerDao;
  import org.rohith.model.Customer;
  
  
  public class CustomerDaoImpl   implements CustomerDao {
  
      private SessionFactory sessionFactory;
  
      @Override
      public void saveCustomer(Customer customer) {
          Session session = getSession();
          session.clear();
          try {
                  session.saveOrUpdate(customer);
                  session.flush();
          } catch (Throwable e) {
              throw new RuntimeException(e.getMessage(), e);
          }
      }
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
      public SessionFactory getSessionFactory() {
          return this.sessionFactory;
      }
  
      public Session getSession() throws HibernateException {
          Session sess = getSessionFactory().getCurrentSession();
          if (sess == null) {
              sess = getSessionFactory().openSession();
          }
  //      Session sess = getSessionFactory().openSession();
          return sess;
      }
  
  }
  </code></pre>
  
  <p><strong>CustomerAction.java</strong></p>
  
  <pre><code>public class CustomerAction extends ActionSupport{
      private String name;
      private String addr1;
      private String addr2;
      private String city;
      private String state;
  
      private CustomerServiceImpl customerServiceImpl;
  
  //Getters and setters
  
      public String execute(){
          Customer cust= new Customer();
          cust.setName(name);
          cust.setAddress1(addr1);
          cust.setAddress2(addr2);
          cust.setCity(city);
          cust.setState(state);
          System.out.println(name);
          WebApplicationContext webApplicationContext = WebApplicationContextUtils
                  .getRequiredWebApplicationContext(getRequest().getSession()
                          .getServletContext());
          customerServiceImpl = (CustomerServiceImpl) webApplicationContext.getBean("customerServiceImpl");
          customerServiceImpl.saveCustomer(cust);
          //saveCust(cust);
          return "success";
      }
  protected HttpServletRequest getRequest() {
          return ServletActionContext.getRequest();
      }
  protected HttpServletResponse getResponse() {
          return ServletActionContext.getResponse();
      }
  }
  </code></pre>
  
  <p>The Exception I am getting</p>
  
  <pre><code>org.hibernate.HibernateException: No Session found for current thread
      org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
      org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
      org.rohith.dao.impl.CustomerDaoImpl.getSession(CustomerDaoImpl.java:33)
      org.rohith.dao.impl.CustomerDaoImpl.saveCustomer(CustomerDaoImpl.java:16)
      org.rohith.service.impl.CustomerServiceImpl.saveCustomer(CustomerServiceImpl.java:18)
      org.rohith.CustomerAction.execute(CustomerAction.java:36)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:606)
  </code></pre>