Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: hibernate4: No Session found for current thread

  1. #11
    Join Date
    Jan 2012
    Posts
    4

    Default

    Is this really fixed? I think I'm still getting this in my spring 3.1/hibernate 4.0 setup... Due to the nature of this problem I have posted my configs on StackOverflow to ensure that the problem doesn't lie with my setup.

    http://stackoverflow.com/questions/8...nd-hibernate-4

    The crux of it is I am still getting the following exception:
    SEVERE: Servlet.service() for servlet [lystra] in context with path [/lystra] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionCo ntext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurre ntSession(SessionFactoryImpl.java:881)

  2. #12
    Join Date
    Nov 2011
    Posts
    2

    Exclamation

    I get the same exception using Spring 3.1.1.RELEASE with Hibernate 4.0.1.Final:
    Code:
    SEVERE: org.hibernate.HibernateException: No Session found for current thread
    javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:679)
    Caused by: org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)
    	at ro.kenjiru.yachin.persistence.HibernateListingDao.currentSession(HibernateListingDao.java:18)
    	at ro.kenjiru.yachin.persistence.HibernateListingDao.addListing(HibernateListingDao.java:22)
    	at ro.kenjiru.yachin.service.ListingServiceImpl.createListing(ListingServiceImpl.java:16)
    	at ro.kenjiru.yachin.controller.ListingBean.addListing(ListingBean.java:35)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:616)
    	at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    	at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    	at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    	... 20 more
    Mar 12, 2012 11:12:08 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
    WARNING: #{listingBean.addListing}: org.hibernate.HibernateException: No Session found for current thread
    javax.faces.FacesException: #{listingBean.addListing}: org.hibernate.HibernateException: No Session found for current thread
    	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    	at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    	at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    	at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    	at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:679)
    Caused by: javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    	at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	... 19 more
    Caused by: org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:883)
    	at ro.kenjiru.yachin.persistence.HibernateListingDao.currentSession(HibernateListingDao.java:18)
    	at ro.kenjiru.yachin.persistence.HibernateListingDao.addListing(HibernateListingDao.java:22)
    	at ro.kenjiru.yachin.service.ListingServiceImpl.createListing(ListingServiceImpl.java:16)
    	at ro.kenjiru.yachin.controller.ListingBean.addListing(ListingBean.java:35)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:616)
    	at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
    	at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    	at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    	at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    	... 20 more
    I have a simple Dao:
    Code:
    @Repository
    public class HibernateListingDao implements ListingDao {
    	@Autowired(required=true)
    	private SessionFactory sessionFactory;
    
    	public HibernateListingDao() { }
    
    	public Session currentSession() {
    		return sessionFactory.getCurrentSession();
    	}
    
    	public void addListing(Listing listing) {
    		currentSession().save(listing);
    	}
    }
    And a very simple service:
    Code:
    @Service
    public class ListingServiceImpl implements ListingService {
    	@Autowired
    	private ListingDao listingDao;
    	
    	@Override
    	public void createListing(Listing listing) {
    		listingDao.addListing(listing);
    	}
    }
    The bean looks like this:
    Code:
    @Component
    @Scope("session")
    public class HelloBean {
    	private String name;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    }
    The application context file is:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	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">
    
    	<context:annotation-config />
    	<context:component-scan base-package="ro.kenjiru.yachin" />
    
    	<bean id="dataSource"
    		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    
    		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    		<property name="url" value="jdbc:hsqldb:mem://localhost/yachin" />
    		<property name="username" value="sa" />
    		<property name="password" value="" />
    	</bean>
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    
    		<property name="dataSource" ref="dataSource" />
    		<property name="annotatedClasses">
    			<list>
    				<value>ro.kenjiru.yachin.domain.Listing</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    			</props>
    		</property>
    	</bean>
    	
    	<bean id="listing" class="ro.kenjiru.yachin.domain.Listing" />
    </beans>
    Am I missing something obvious here?

  3. #13
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You have nothing to drive your transactions, read the reference guide especially the transaction section and take a look at the reference/sample application(s).
    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

  4. #14
    Join Date
    Jan 2012
    Posts
    6

    Default

    I have a trouble, please help to resolve it!

    Application context
    Code:
    <?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: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-3.1.xsd">
    
        <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/security"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
        </bean>
    
        <bean name="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="packagesToScan">
                <list>
                    <value>name.shoo.springsecurity.entities</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                </props>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <tx:annotation-driven/>
    
    </beans>
    DAO Implement
    Code:
    @Repository("usersDAO")
    public class UsersDAOImpl implements UsersDAO{
    
        @Resource(name="sessionFactory")
        SessionFactory sessionFactory;
    
        public String getSomething() {
            return (String)sessionFactory.getCurrentSession().toString();
        }
    }
    Service
    Code:
    @Service("usersService")
    public class UsersService {
    
        @Resource(name = "usersDAO")
        UsersDAO usersDAO;
    
        @Transactional
        public String doSomething() {
            return (String)usersDAO.getSomething().toString();
        }
    
    }
    Controller
    Code:
    @Controller
    public class HomeController {
    
        @Resource(name="usersService")
        UsersService usersService;
    
        @RequestMapping("/")
        public String home() {
    
            System.out.println(usersService.doSomething());
    
            return "index";
        }
    And there is ERROR:
    Code:
    апр 18, 2012 4:50:45 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread] with root cause
    org.hibernate.HibernateException: No Session found for current thread
    	at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
    	at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1039)
    	at name.shoo.springsecurity.dao.UsersDAOImpl.getSomething(UsersDAOImpl.java:31)
    	at name.shoo.springsecurity.services.UsersService.doSomething(UsersService.java:24)
    	at name.shoo.springsecurity.controllers.HomeController.home(HomeController.java:28)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    	at java.lang.Thread.run(Thread.java:722)

  5. #15
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use the search as this question has been answered numerous times before...

    Judging from your stacktrace and code you use component scanning and have duplicate components. How to overcome that is explained in the reference guide and answered many times on the forum 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

  6. #16
    Join Date
    Jan 2012
    Posts
    6

    Cool

    Quote Originally Posted by Marten Deinum View Post
    Please use the search as this question has been answered numerous times before...

    Judging from your stacktrace and code you use component scanning and have duplicate components. How to overcome that is explained in the reference guide and answered many times on the forum before.
    Aww! That's fantastic! Just moving <tx:annotation-driven/> from applicationContext to dispatcherContext and it working now! Thank's!

    That's info from documentation was helpful for me:
    Note
    <tx:annotation-driven/> only looks for @Transactional on beans in the same application context it is defined in. This means that, if you put <tx:annotation-driven/> in a WebApplicationContext for a DispatcherServlet, it only checks for @Transactional beans in your controllers, and not your services. See Section 16.2, “The DispatcherServlet” for more information.
    Last edited by shoo; Apr 19th, 2012 at 07:20 AM.

Tags for this Thread

Posting Permissions

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