View Full Version : Spring Hibernate Integration Problem in SessionFactory TransactionManager
ankitmishra
Jul 3rd, 2012, 11:15 AM
Hello Friends,
I was working on a basic spring+hibernate integration application.
Getting below error
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletExceptio n: 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
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.ob tainContent(SiteMeshFilter.java:129)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.do Filter(SiteMeshFilter.java:77)
root cause
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.AbstractSession FactoryBean$TransactionAwareInvocationHandler.invo ke(AbstractSessionFactoryBean.java:270)
$Proxy12.getCurrentSession(Unknown Source)
com.gloria.dao.impl.StudentDaoImpl.registerStudent (StudentDaoImpl.java:19)
com.gloria.service.impl.StudentServiceImpl.registe rStudent(StudentServiceImpl.java:20)
com.gloria.app.RegisterController.home(RegisterCon troller.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:592)
org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:212 )
org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:126)
ankitmishra
Jul 3rd, 2012, 11:17 AM
Hello Friends,
I was working on a basic spring+hibernate integration application.
Getting below error
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletExceptio n: 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
org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.ob tainContent(SiteMeshFilter.java:129)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.do Filter(SiteMeshFilter.java:77)
root cause
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.AbstractSession FactoryBean$TransactionAwareInvocationHandler.invo ke(AbstractSessionFactoryBean.java:270)
$Proxy12.getCurrentSession(Unknown Source)
com.gloria.dao.impl.StudentDaoImpl.registerStudent (StudentDaoImpl.java:19)
com.gloria.service.impl.StudentServiceImpl.registe rStudent(StudentServiceImpl.java:20)
com.gloria.app.RegisterController.home(RegisterCon troller.java:41)
sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:592)
org.springframework.web.method.support.InvocableHa ndlerMethod.invoke(InvocableHandlerMethod.java:212 )
org.springframework.web.method.support.InvocableHa ndlerMethod.invokeForRequest(InvocableHandlerMetho d.java:126)
StudentDaoImpl.java
----------------------
package com.gloria.dao.impl;
import com.gloria.dao.StudentDao;
import com.gloria.form.Student;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autow ired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transac tional;
@Repository
public class StudentDaoImpl implements StudentDao
{
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void registerStudent(Student student)
{
sessionFactory.getCurrentSession().save(student);
}
}
ankitmishra
Jul 3rd, 2012, 11:18 AM
<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.gloria.app" />
<context:component-scan base-package="com.gloria.dao" />
<context:component-scan base-package="com.gloria.dao.impl" />
<context:component-scan base-package="com.gloria.form" />
<context:component-scan base-package="com.gloria.hibernate" />
<context:component-scan base-package="com.gloria.service" />
<context:component-scan base-package="com.gloria.service.impl" />
<context:component-scan base-package="com.gloria.util" />
ankitmishra
Jul 3rd, 2012, 11:19 AM
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<context:annotation-config />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewR esolver">
<beans:property name="viewClass">
<beans:value>
org.springframework.web.servlet.view.tiles2.TilesV iew
</beans:value>
</beans:property>
</beans:bean>
ankitmishra
Jul 3rd, 2012, 11:20 AM
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost/gloria" />
<beans:property name="username" value="root" />
<beans:property name="password" value="developer" />
</beans:bean>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
ankitmishra
Jul 3rd, 2012, 11:21 AM
<beans: property name="dataSource" ref="dataSource" />
<beans: property name="configLocation">
<beans:value>classpath:hibernate.cfg.xml</beans:value>
</beans: property>
<beans: property name="configurationClass">
<beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
</beans: property>
ankitmishra
Jul 3rd, 2012, 11:35 AM
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<beans property name="dataSource" ref="dataSource" />
<beans : property name="configLocation">
<beans:value>classpath:hibernate.cfg.xml</beans:value>
</beans : property>
<beans : property name="configurationClass">
<beans:value>org.hibernate.cfg.AnnotationConfiguration</beans:value>
</beans : property>
<beans : property name="hibernateProperties">
<beans : props>
<beans : prop key="hibernate.dialect">${jdbc.dialect}</beans : prop>
<beans : prop key="hibernate.show_sql">true</beans : prop>
</beans : props>
</beans : property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<beans : property name="sessionFactory" ref="sessionFactory" />
</beans:bean>
</beans:beans>
Marten Deinum
Jul 3rd, 2012, 01:42 PM
If you want help please obey forum rules and when posting code use [ code][/code ] tags that way it remains readable. Also use the forum search as this question has been answered numerous times before.
1) Don't mess around with the session context property
2) There is nothing that drives your transaction (no tx:annotation-driven)
3) Don't scan for components twice
But as stated use the search as this question has been answered numerous times before.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.