Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: No Session found for current Thread (Hibernate 4/Spring3)

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default No Session found for current Thread (Hibernate 4/Spring3)

    well hello there
    I think it's been a week since i first got to this error while setting up my first WebApp working with Database, since then it's been lot of time rewriting the code in billion ways , trying everything i could find on internet but still nothing ...

    This is the stack trace
    SEVERE: Servlet.service() for servlet [spring] in context with path [/WebAppDB] 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:941)
    	at webapp.dao.DAOUser.getUser(DAOUser.java:21)
    	at webapp.service.ServiceUser.getUser(ServiceUser.java:17)
    	at webapp.controller.MainController.showCar(MainController.java:27)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    	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:240)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)

    and following are my source and configuration files (for now i'll show you only those i expect to contain the error i cannot find, ask for more if you think the error might be there)

    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 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_3_0.xsd">
     
        <display-name>WebAppDB</display-name>
     
        <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>      
        </servlet>
     
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
           					   [url]http://www.springframework.org/schema/beans/spring-beans-3.2.xsd[/url] 
           					   [url=http://www.springframework.org/schema/mvc]Index of /schema/mvc[/url] 
           					   [url]http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd[/url] 
           					   [url=http://www.springframework.org/schema/context]Index of /schema/context[/url] 
           					   [url]http://www.springframework.org/schema/context/spring-context-3.2.xsd[/url]
           					   [url=http://www.springframework.org/schema/tx]Index of /schema/tx[/url]
           					   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
     
    	<context:component-scan base-package="webapp.service, webapp.dao" />
     
    	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	</bean>
     
    	<bean id="mySessionFactory"
    		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
    		<property name="configLocation">
    			<value>classpath:hibernate.cfg.xml</value>
    		</property>
    		<property name="hibernateProperties">
    			<value>
    				org.hibernate.dialect.MySQLDialect
    			</value>
    		</property>
    	</bean>
     
    	<bean id="transactionManager"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource">
    			<ref local="myDataSource" />
    		</property>
    	</bean>
     
    	<tx:annotation-driven transaction-manager="transactionManager"
    		proxy-target-class="true" />
     
    </beans>

    DAOUser.java
    package webapp.dao;
     
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
     
    import webapp.entity.User;
     
    @Repository
    public class DAOUser {
     
    	@Autowired
    	private SessionFactory factory;
     
    	public User getUser() {
     
    		return (User) factory.getCurrentSession().createQuery("FROM User").list().get(0);
    	}
    }

    serviceUser.java
    package webapp.service;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
     
    import webapp.entity.*;
    import webapp.dao.*;
     
    @Service
    public class ServiceUser {
     
    	@Autowired
    	private DAOUser daoUser;
     
    	@Transactional	
    	public User getUser() {
     
    	return daoUser.getUser();	
    	}
    }

    note that the error shows up even if i change my login data i.e. if i try to set datasource with nonexistent username or wrong password for root etc. I thought the error was somewhere there but with the same hibernate configuration which are shown in appContext.xml i can access the database from different (not web app) project


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: No Session found for current Thread (Hibernate 4/Spring3)

    Again, you might want to link to the crossposts of this thread. People aren't going to bother answering questions that might already have answers elsewhere.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. No Session found for current Thread (Hibernate 4/Spring3)
    By TheJudge in forum Web Frameworks
    Replies: 5
    Last Post: August 5th, 2014, 04:49 PM
  2. [SOLVED] "org.hibernate.Session" ClassNotFoundException
    By talha06 in forum Exceptions
    Replies: 3
    Last Post: August 21st, 2013, 11:13 AM
  3. no constructor found for thread
    By aaasaakdx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2013, 01:14 PM
  4. Replies: 4
    Last Post: August 8th, 2011, 01:09 PM
  5. Stopping All Current Thread
    By newbie in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 08:27 PM

Tags for this Thread