title: org.hibernate.HibernateException: No Session found
body: <p>Hi I am facing a very strange problem. I am using Spring MVC 3.0+Spring Security 3.2+hibernate 4.3. before login I am able to hit database and getting entity befor login. But after login by spring security, I am getting exception <code>org.hibernate.HibernateException: No Session found for current thread</code>. This exception thrown on <code>sessionFactory.getCurrentSession()</code></p>
  
  <p>I have used transection management by AOP. My question is: </p>
  
  <p><strong>Why excpetion come after login? While it is working fine before login?</strong></p>
  
  <p>My Code is as below:</p>
  
  <pre><code>@Repository("categoryDAO")
  public class CategoryDAOHibernateImpl implements CategoryDAO {
  
  @Autowired
  SessionFactory sessionFactory;
  
  public List&lt;Category&gt; findAll() {
      List&lt;Category&gt; list;
      try {
          Session session = sessionFactory.getCurrentSession();
          Criteria criteria = session.createCriteria(Category.class);
          list = criteria.list();
      } catch (Exception e) {
          throw new BookStoreDAORuntimeException(e.getMessage(), e);
      }
      return list;
  }
  }
  </code></pre>
  
  <p>FullStacktrace:</p>
  
  <pre><code>  StandardWrapperValve[appServlet]: Servlet.service() for servlet appServlet threw exception
   org.hibernate.HibernateException: No Session found for current thread
  at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
  at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1012)
  at com.abhendra.bookstore.common.entities.dao.hibernate.UserDAOHibernateImpl.findByEmail(UserDAOHibernateImpl.java:68)
  at com.abhendra.bookstore.management.user.impl.UserManagementServiceImpl.findByEmail(UserManagementServiceImpl.java:20)
  at com.abhendra.bookstore.LibreryController.showCategory(LibreryController.java:27)
  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:606)
  at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
  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:748)
  at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
  at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
  at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
  at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
  at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
  at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
  at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
  at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
  at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
  at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
  at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
  at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
  at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
  at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
  at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
  at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
  at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
  at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
  at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
  at java.lang.Thread.run(Thread.java:744)
  ]]
  </code></pre>
  
  <p>daoContext.xml</p>
  
  <pre><code>&lt;bean id="dataSource"
      class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName"&gt;
          &lt;value&gt;org.postgresql.Driver&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="url"&gt;
          &lt;value&gt;jdbc:postgresql://localhost:5432/bookstore&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="username"&gt;
          &lt;value&gt;bookstore&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;bookstore&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref bean="dataSource" /&gt;
      &lt;/property&gt;
      &lt;property name="mappingResources"&gt;
          &lt;list&gt;
              &lt;value&gt;Author.hbm.xml&lt;/value&gt;
              &lt;value&gt;Book.hbm.xml&lt;/value&gt;
              &lt;value&gt;Category.hbm.xml&lt;/value&gt;
              &lt;value&gt;Comment.hbm.xml&lt;/value&gt;
              &lt;value&gt;Download.hbm.xml&lt;/value&gt;
              &lt;value&gt;Rating.hbm.xml&lt;/value&gt;
              &lt;value&gt;Role.hbm.xml&lt;/value&gt;
              &lt;value&gt;User.hbm.xml&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="hibernateProperties"&gt;
          &lt;value&gt;
              hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
              jdbc.use_getGeneratedKeys=true
              hibernate.show_sql=true
              hibernate.hbm2ddl.auto=update
              hibernate.generate_statistics=false
  
              hibernate.cache.use_second_level_cache=false
              hibernate.cache.use_query_cache=false
              hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
          &lt;/value&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory"&gt;
          &lt;ref bean="sessionFactory" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;tx:advice id="defaultTxAdvice" transaction-manager="transactionManager"&gt;
      &lt;tx:attributes&gt;
          &lt;tx:method name="get*" read-only="true" propagation="REQUIRED"/&gt;
          &lt;tx:method name="find*" read-only="true" propagation="REQUIRED"/&gt;
          &lt;tx:method name="*" /&gt;
      &lt;/tx:attributes&gt;
  &lt;/tx:advice&gt;
  
  
  
  &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;aop:config&gt;
      &lt;aop:pointcut id="dbServiceOperations"
          expression="execution(* com.abhendra.bookstore.common.entities.dao.*DAO*.*(..))" /&gt;
      &lt;aop:advisor pointcut-ref="dbServiceOperations" advice-ref="defaultTxAdvice" /&gt;
  &lt;/aop:config&gt;
  </code></pre>
  
  <p>SecurityConfig:</p>
  
  <pre><code>&lt;security:http auto-config="false" use-expressions="true"&gt;
      &lt;security:form-login login-page="/bookstore/authentication/login"
          login-processing-url="/bookstore/authentication/loginProcess"
          default-target-url="/bookstore/home/index"
          authentication-failure-url="/bookstore/authentication/login?login_error=1" /&gt;
      &lt;!-- authentication-failure-handler-ref="postFailedAuthHandler" --&gt;
      &lt;security:logout logout-url="/bookstore/authentication/logout"
          logout-success-url="/bookstore/authentication/login" /&gt;
      &lt;security:intercept-url pattern="/bookstore/admin/**" access="isAuthenticated()" /&gt;
      &lt;security:intercept-url pattern="/bookstore/home/**" access="isAuthenticated()" /&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="authenticationProvider" class="com.abhendra.core.spring.security.BookstoreAuthenticationProvider" /&gt;
  
  &lt;security:authentication-manager alias="authenticationManager" erase-credentials="false"&gt;
      &lt;security:authentication-provider ref="authenticationProvider"&gt;&lt;/security:authentication-provider&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>rootContext.xml</p>
  
  <pre><code>&lt;import resource="classpath:daoContext.xml" /&gt;
  
  &lt;context:component-scan base-package="com.abhendra.bookstore, com.abhendra.core" /&gt;
  </code></pre>
  
  <p>web.xml</p>
  
  <pre><code>&lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security-config.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;filter&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;filter-class&gt;
          org.springframework.web.filter.DelegatingFilterProxy
      &lt;/filter-class&gt;
  &lt;/filter&gt;   
  
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
      &lt;url-pattern&gt;/bookstore/*&lt;/url-pattern&gt;
      &lt;dispatcher&gt;REQUEST&lt;/dispatcher&gt;    
      &lt;dispatcher&gt;FORWARD&lt;/dispatcher&gt;
  &lt;/filter-mapping&gt;
  
  &lt;!-- Map all /resources requests to the Resource Servlet for handling --&gt;
  &lt;!-- &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Resources Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt; --&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/bookstore/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Thanks In Advance.:-)</p>