我正在尝试使用Spring MVC 3和Hibernate 4.1来获取声明式事务管理,为整个请求提供一个开放会话。我只有一个“管理器”层用于数据访问,它直接使用会话。这是用@Transactional注释的,会话在退出该层时关闭,我在控制器中得到一个延迟加载异常。
所以我添加了OpenSessionInViewFilter过滤器,并且日志声明此过滤器已正确配置,并且它们还显示此过滤器打开会话。不幸的是,我的经理层无论如何都会打开并关闭自己的会话,就像之前一样,我在控制器中得到了相同的延迟加载异常。
编辑:注意在我的日志中,我得到过滤器会话打开,HibernateTransactionManager会话打开,HibernateTransactionManager会话关闭,延迟加载异常,然后过滤器会话关闭。所以我知道在延迟加载异常期间某处有一个打开的会话,但该对象是在与另一个已关闭的会话关联的事务中加载的。我认为从管理器类中删除@Transactional将从该层中删除会话管理并让过滤器完成其工作,但是sessionFactory()。getCurrentSession()只是获得一个关闭的会话;它无权访问过滤器提供的打开会话。
如何才能访问OpenSessionInViewFilter明确开放的会话?
springapp-servlet.xml中
<beans ..
<context:component-scan base-package="springapp.web" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<bean id="userManager" class="springapp.service.SimpleUserManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<import resource="hibernate-context.xml" />
</beans>
的web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml</param-value>
</context-param>
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
冬眠-context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans ..
">
<context:property-placeholder location="/WEB-INF/hibernate.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${hibernate.connection.driver_class}" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven />
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
</property>
</bean>
<bean id="managerTemplate" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="databeans" />
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
</beans>
manager.java
@Service("userManager")
@Transactional
public class SimpleUserManager implements UserManager {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
..
controller.java
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserManager userManager;
..
和我的异常,发生在控制器中,读取在manager类中加载并传递给该控制器的对象的第一个属性:
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.hibernate.LazyInitializationException:无法初始化代理 - 没有Session org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
我一直在研究这个问题几个小时..我所能找到的是使用hibernate 3的混合解决方案和hibernate 4会话/事务理论的粒状解释,没有示例。
有什么想法吗?
更新:固定!
发现这个:
Spring MVC OpenSessionInViewInterceptor Not Working
很遗憾没有帮助那里的OP,但它帮助了我。值得注意的是,这个:“最好不要在dispatcher-servlet.xml中导入applicationContext.xml,而是使用ContextLoaderListener加载它”
,应用于我的配置,正是我上面发布的所有内容,而不是
<import resource="hibernate-context.xml" />
在我的springapp-servlet.xml中,我将ContextLoaderListener修改为:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springapp-servlet.xml, /WEB-INF/hibernate-context.xml</param-value>
</context-param>
这就是
所需的一切sessionFactory.getCurrentSession()
将OSIV创建的会话返回给我。而且,事实上,正在创建的第二个会话不再是,因为我发现了这些可爱的小日志行:
2012-10-04 14:43:48,743 DEBUG http-8080-1 org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'transactionManager'
2012-10-04 14:43:48,743 TRACE http-8080-1 org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value [org.springframework.orm.hibernate4.SessionHolder@4146c5c0] for key [org.hibernate.internal.SessionFactoryImpl@12542011] bound to thread [http-8080-1]
2012-10-04 14:43:48,743 DEBUG http-8080-1 org.springframework.orm.hibernate4.HibernateTransactionManager - Found thread-bound Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] for Hibernate transaction
2012-10-04 14:43:48,743 DEBUG http-8080-1 org.springframework.orm.hibernate4.HibernateTransactionManager - Creating new transaction with name [springapp.service.SimpleUserManager.find]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
表明,在我的@Transactional服务层之外,OSIV创建的会话还活着,而且只是为它启动了一个新事务。
希望这有助于其他人。我甚至不想考虑花多少时间来处理它。