Results 1 to 8 of 8

Thread: Spring Hibernate Integration Problem in SessionFactory TransactionManager

  1. #1
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default Spring Hibernate Integration Problem in SessionFactory TransactionManager

    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)

  2. #2
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    Quote Originally Posted by ankitmishra View Post
    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);
    }
    }

  3. #3
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    <?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/schem...ng-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-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" />

  4. #4
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    <!-- 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.Intern alResourceViewResolver">
    <beansroperty name="prefix" value="/WEB-INF/views/" />
    <beansroperty name="suffix" value=".jsp" />
    </beans:bean>
    <beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBas edViewResolver">
    <beansroperty name="viewClass">
    <beans:value>
    org.springframework.web.servlet.view.tiles2.TilesV iew
    </beans:value>
    </beansroperty>
    </beans:bean>

  5. #5
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beansroperty name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beansroperty name="url" value="jdbc:mysql://localhost/gloria" />
    <beansroperty name="username" value="root" />
    <beansroperty name="password" value="developer" />
    </beans:bean>

    <beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">

  6. #6
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    <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.AnnotationConfigura tion</beans:value>
    </beans: property>

  7. #7
    Join Date
    Jul 2012
    Location
    New Delhi
    Posts
    9

    Default

    <beans:bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <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.AnnotationConfigura tion</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.Hibernat eTransactionManager">
    <beans : property name="sessionFactory" ref="sessionFactory" />
    </beans:bean>

    </beans:beans>

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •