-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Spring OpenSessionInViewFilter, no hibernate session?
PostPosted: Tue Sep 15, 2009 11:46 am 
Newbie

Joined: Thu Aug 06, 2009 10:59 am
Posts: 6
I've been using OpenSessionInViewInterceptor in my Spring MVC application to make use of Hibernate and have so far got most things running. I'm now trying to use my Hibernate DAOs to retrieve user data for Spring Security (when logging in and switching users), however I'm having trouble with this exception:

Code:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here


I've a feeling this could be caused by the Hibernate session not being ready when Spring Security filters the request (as Spring Security filters the request at the servlet/web.xml level). So I turned to OpenSessionInViewFilter which I have defined before the Spring Security filter in my web.xml file so it should be called first. In my web.xml file I have the following:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>LiveFrontend</display-name>

   <servlet>
      <description>WebFrontend Dispatcher Servlet</description>
      <servlet-name>WebFrontend</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
   </servlet>

   <servlet-mapping>
      <servlet-name>WebFrontend</servlet-name>
      <url-pattern>*.do</url-pattern>
   </servlet-mapping>

   <filter>
      <filter-name>hibernateFilter</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
      <init-param>
         <param-name>sessionFactoryBeanName</param-name>
         <param-value>hibernateSessionFactory</param-value>
      </init-param>
   </filter>

   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
         /WEB-INF/WebFrontend-servlet.xml
      </param-value>
   </context-param>

   <filter>
      <filter-name>springSecurityFilterChain</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   </filter>

   <filter-mapping>
     <filter-name>hibernateFilter</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping>
   <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>


However if I use this then I cannot access the hibernate session through SessionFactory.getCurrentSession(), but I can use HibernateDaoSupport.find() etc. It's as though it simply isn't setting up a Hibernate session.

Any ideas?


Top
 Profile  
 
 Post subject: Re: Spring OpenSessionInViewFilter, no hibernate session?
PostPosted: Sat Dec 12, 2009 11:11 am 
Newbie

Joined: Sat Dec 12, 2009 10:55 am
Posts: 1
Did you ever find a solution to this problem?

I have the following error stack that looks similar:

Code:
    org.hibernate.HibernateException: No session currently bound to execution context
    org.hibernate.context.ManagedSessionContext.currentSession(ManagedSessionContext.java:74)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
    com.vicinity.dao.hibernate.GenericHibernateDAO.findByCriteria(GenericHibernateDAO.java:99)
    com.vicinity.dao.hibernate.HibernateUserDAO.getUserByLogin(HibernateUserDAO.java:35)
    com.vicinity.service.PersistentUserManager.loadUserByUsername(PersistentUserManager.java:67)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    $Proxy70.loadUserByUsername(Unknown Source) org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:83)
    org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:125)
    org.springframework.security.authentication.ProviderManager.doAuthentication(ProviderManager.java:121)
    org.springframework.security.authentication.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:49)
    org.springframework.security.authentication.ProviderManager.doAuthentication(ProviderManager.java:139)
    org.springframework.security.authentication.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:49)
    org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:98)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
    org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:108)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:150)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

You can see more details about my configuration on Stackoverflow.com.
I've even attempted using the OpenSessionInViewFilter like you have but again to no avail. Hitting a wall with this one and there doesn't seem to be much help put there so any thoughts would be appreciated. Thanks.


Top
 Profile  
 
 Post subject: Re: Spring OpenSessionInViewFilter, no hibernate session?
PostPosted: Sun Dec 13, 2009 9:09 pm 
Newbie

Joined: Thu Aug 06, 2009 10:59 am
Posts: 6
Unfortunately I no longer have access to the Spring/Hibernate work that I did but if memory serves me correctly I think I ended up having to do raw queries to load up the user data for Spring Security. This isn't a good solution by any means but time constraints meant we had to do it this way.

Sorry I couldn't be of more help.


Top
 Profile  
 
 Post subject: Re: Spring OpenSessionInViewFilter, no hibernate session?
PostPosted: Thu Jun 24, 2010 6:03 am 
Newbie

Joined: Thu Jun 24, 2010 5:56 am
Posts: 1
NickB wrote:
Unfortunately I no longer have access to the Spring/Hibernate work that I did but if memory serves me correctly I think I ended up having to do raw queries to load up the user data for Spring Security. This isn't a good solution by any means but time constraints meant we had to do it this way.

Sorry I couldn't be of more help.


Hi, I managed to fixed this problem. I think it is because you need to reattach the entity to the session. I've created a method reattach entity to session which does the following:

@Override
public Entity reattachEntityToSession(Entity entity) {
return (Entity) this.getHibernateTemplate().merge(entity);
}

when entity is the specific object that you wish to access within the spring session.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.