No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

Problem :

DEBUG: org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.hibernate.HibernateException: No Hibernate Session bound to thread,
and configuration does not allow creation of non-transactional one here

This is the exception i had found when i throw away the HibernateSupportDao. I tried the spring recipes chapter 9 example by using the Persistence Objects with Hibernate’s Contextual Sessions. When i run the example by just create a main class it will work fine. This only happen when i create a spring mvc annotation base application. I’m not so sure about the reason behind, i suspect the configuration for webapplicationcontext and application context. But anyway it work finally at my last tried.

Failed Configuration and Sample Code

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

	<display-name>Pro Spring OSGI</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring-hibernate.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<servlet-name>ResourcesServlet</servlet-name>
		<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
	</servlet>

	

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>	

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>	

	<resource-ref>
		<description>Database Connection</description>
		<res-ref-name>jdbc/osgi</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>
</web-app>


spring-hibernate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 	xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		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-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

			http://www.springframework.org/schema/context                    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

	<context:component-scan base-package="com.lence"/>

	<context:annotation-config/>

 	<tx:annotation-driven/> 	

	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/osgi"/>
		<property name="username" value="root"/>
		<property name="password" value=""/>
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<property name="annotatedClasses">
			<list>
				
				<value>com.lence.portal.person.model.Person</value>
				<value>com.lence.portal.member.model.Member</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory" />
        </bean>

	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />	

</beans>


PersonDao.java

package com.lence.portal.person.dao;
import java.util.List;
import com.lence.portal.person.model.Person;

public interface PersonDao {

	public void store(Person person);

	public void delete(Long personId);

	public Person findById(Long id);

	public List<Person> findAll();
}



HibernatePersonDaoImpl.java


package com.lence.portal.person.dao.impl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.lence.portal.person.dao.PersonDao;
import com.lence.portal.person.model.Person;

@Repository("personDao")
public class HibernatePersonDaoImpl implements PersonDao {

	private SessionFactory sessionFactory;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}	

	@Transactional
	public void delete(Long personId) {
		Person person = (Person) sessionFactory.getCurrentSession().get(Person.class,
				personId);
		sessionFactory.getCurrentSession().delete(person);
	}

	@Transactional(readOnly = true)
	public List findAll() {
		Query query = sessionFactory.getCurrentSession().createQuery("from Person");
		return query.list();
	}

	@Transactional(readOnly = true)
	public Person findById(Long id) {
		return (Person) sessionFactory.getCurrentSession().get(Person.class, id);
	}

	@Transactional
	public void store(Person person) {
		System.out.println("asdasd");
		sessionFactory.getCurrentSession().saveOrUpdate(person);
	}
}



PersonService.java


package com.lence.portal.person.service;
import java.util.List;
import com.lence.portal.person.model.Person;

public interface PersonService {

	public void store(Person person);

	public void delete(Long personId);

	public Person findById(Long id);

	public List findAll();
}


PersonServiceImpl.java


package com.lence.portal.person.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lence.portal.person.dao.PersonDao;
import com.lence.portal.person.model.Person;
import com.lence.portal.person.service.PersonService;

@Service("personService")
public class PersonServiceImpl implements PersonService {

	@Autowired PersonDao personDao;

	public void store(Person person){
		personDao.store(person);
	}

	public void delete(Long personId){
		personDao.delete(personId);
	}

	public Person findById(Long id){
		return personDao.findById(id);
	}

	public List findAll(){
		return personDao.findAll();
	}
}


PersonController.java

package com.lence.portal.person.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.lence.portal.person.model.Person;
import com.lence.portal.person.service.PersonService;

@Controller
@RequestMapping("/person.htm")
@SessionAttributes("person")
public class PersonController {

	@Autowired private PersonService personService;

	@ModelAttribute("person")
	public Person populatePerson(){
		return new Person();
	}

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(HttpServletRequest request, ModelMap model ) {

		return "/person/person";
	}	

	@RequestMapping(method = RequestMethod.POST)
	public String submitForm(@ModelAttribute("person")Person person, Errors errors,
			HttpServletRequest request, HttpServletResponse response, ModelMap model ) throws Exception {
		personService.store(person);
		return "/person/person";
	}
}


Solution tried

I believe the problem come from the configuration together with the dispatchServlet and root context problem. I make the following changes and the program work fine finally.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
		version="2.5">

	<display-name>Pro Spring OSGI</display-name>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring-servlet.xml
			/WEB-INF/spring-hibernate.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/spring-servlet.xml
				/WEB-INF/spring-hibernate.xml
			</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet>
		<servlet-name>ResourcesServlet</servlet-name>
		<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
	</servlet>

	
	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<resource-ref>
		<description>Database Connection</description>
		<res-ref-name>jdbc/osgi</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

</web-app>
You can leave a response, or trackback from your own site.

One Response to “No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here”

  1. Arun says:

    Thanks For this Post.
    its vary useful for me because i was also facing same problem .
    now i solved my problem using this post

Leave a Reply

Security Code: