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?