title: org.hibernate.HibernateException: No Session found for current thread 5
body: <p>I know this problem is asked a lot but I am stuck. I am basing my project off of this tutorial: <a href="http://www.cavalr.com/blog/Spring_3_and_Annotation_Based_Hibernate_4_Example" rel="nofollow">http://www.cavalr.com/blog/Spring_3_and_Annotation_Based_Hibernate_4_Example</a></p>
  
  <p>This is my root-context.xml</p>
  
  <pre class="lang-xml prettyprint-override"><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"
         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"&gt;
  
    &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
      &lt;property name="driverClass" value="${jdbc.driver}"/&gt;
      &lt;property name="jdbcUrl" value="${jdbc.url}"/&gt;
      &lt;property name="user" value="${jdbc.username}"/&gt;
      &lt;property name="password" value="${jdbc.password}"/&gt;
      &lt;property name="initialPoolSize" value="1" /&gt;
      &lt;property name="minPoolSize" value="1" /&gt;
      &lt;property name="maxPoolSize" value="20" /&gt;
    &lt;/bean&gt;
  
    &lt;bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
        &lt;list&gt;
          &lt;value&gt;classpath:db.properties&lt;/value&gt;
        &lt;/list&gt;
      &lt;/property&gt;
    &lt;/bean&gt;
  
    &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="packagesToScan" value="com.joe.recipes.data" /&gt;
      &lt;property name="hibernateProperties"&gt;
        &lt;props&gt;
          &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQL5InnoDBDialect&lt;/prop&gt;
          &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
        &lt;/props&gt;
  
      &lt;/property&gt;
    &lt;/bean&gt;
  
    &lt;!-- Enables the Hibernate @Transactional programming model --&gt;
    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
    &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
    &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>This is my servlet-context.xml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:beans="http://www.springframework.org/schema/beans"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:mvc="http://www.springframework.org/schema/mvc"
               xsi:schemaLocation="http://www.springframework.org/schema/mvc 
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
    &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt;
    &lt;context:component-scan base-package="com.joe.recipes" /&gt;
    &lt;!-- Enables the Spring MVC @Controller programming model --&gt;
    &lt;mvc:annotation-driven /&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;
  
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>This is my AbstractDaoImpl</p>
  
  <pre><code>public abstract class AbstractDaoImpl&lt;E, I extends Serializable&gt; implements AbstractDao&lt;E,I&gt; {
  
      private Class&lt;E&gt; entityClass;
  
      protected AbstractDaoImpl(Class&lt;E&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public Session getCurrentSession() {
          return sessionFactory.getCurrentSession();
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public E findById(I id) {
          return (E) getCurrentSession().get(entityClass, id);
      }
  
      @Override
      public void saveOrUpdate(E e) {
          getCurrentSession().saveOrUpdate(e);
      }
  
      @Override
      public void delete(E e) {
          getCurrentSession().delete(e);
      }
  
      @Override
      public List findByCriteria(Criterion criterion) {
          Criteria criteria = getCurrentSession().createCriteria(entityClass);
          criteria.add(criterion);
          return criteria.list();
      }
  }
  </code></pre>
  
  <p>This is my <code>RecipeDaoImpl</code> class</p>
  
  <pre><code>@Repository
  public class RecipeDaoImpl extends AbstractDaoImpl&lt;Recipe, String&gt; implements RecipeDao {
  
      protected RecipeDaoImpl() {
          super(Recipe.class);
      }
  
      @Override
      public boolean saveRecipe(Recipe r) {
          return saveRecipe(r);
      }
  
      @Override
      public Recipe getRecipe(String recipeId) {
          return findById(recipeId);
      }
  
      @SuppressWarnings("unchecked")
      @Override
      public List&lt;Recipe&gt; findRecipes(String keyword) {
          return findByCriteria( Restrictions.and( Restrictions.like("name", keyword, MatchMode.ANYWHERE),
              Restrictions.like("keywords", keyword, MatchMode.ANYWHERE) ) );
      }
  }
  </code></pre>
  
  <p>This is my <code>RecipeServiceImpl</code> class</p>
  
  <pre><code>@Service("recipeService")
  @Transactional(readOnly = true)
  public class RecipeServiceImpl implements RecipeService {
      @Autowired
      private RecipeDao recipeDao;
  
      @Override
      @Transactional(readOnly = false)
      public boolean saveRecipe(Recipe r) {
          return recipeDao.saveRecipe(r);
      }
  
      @Override
      public Recipe getRecipe(String recipeId) {
          return recipeDao.getRecipe(recipeId);
      }
  
      @Override
      public List&lt;Recipe&gt; findRecipes(String keyword) {
          return recipeDao.findRecipes(keyword);
      }
  }
  </code></pre>
  
  <p>This is my <code>RecipeController</code></p>
  
  <pre><code>/**
   * Handles requests for the application home page.
   */
  @Controller
  public class RecipeController {
  
      private static final Logger logger = LoggerFactory.getLogger(RecipeController.class);
  
      @Autowired
      private RecipeService recipeService;
  
      /**
       * Adds recipes to the DB
       */
      @RequestMapping(value = "/add", method = RequestMethod.GET)
      public String add(Locale locale, Model model) {
          return "add";
      }
  
      /**
       * Searches for recipes
       */
      @RequestMapping(value = "/search", method = RequestMethod.POST)
      public String search(@RequestParam(value="keyword", required=true) String keyword, Model model) {
  
          List&lt;Recipe&gt; recipes = recipeService.findRecipes(keyword);
          System.out.println( "Results:"+ recipes.size() );
          return "results";
      }
  
      /**
       * Logs in the user
       */
      @RequestMapping(value = "/login", method = RequestMethod.POST)
      public String login(Locale locale, Model model) {
  
          return "login";
      }
  }
  </code></pre>
  
  <p>I've tried putting the <code>@Transactional</code> on the <code>RecipeDaoImpl</code> class and the <code>AbstractDaoImpl</code> class and neither worked.</p>
  
  <p>EDIT:
  I fixed this by catching the exception and opening a new one:</p>
  
  <pre><code>public Session getCurrentSession() {
      Session session = null;
      try { 
          session = sessionFactory.getCurrentSession();
      } catch ( HibernateException he ) {
          session = sessionFactory.openSession();
      }
      return session;
  }
  </code></pre>