一、与事务相关的错误
我是用的hibernete版本是:4.3.5.Final,在Junit测试的时候没有问题,可是部署在tomcat上进行页面交互的时候抛出了:
Could not obtain transaction-synchronized Session for current thread的错误,解决方法有两种。
解决方法1:
在web.xml配置一个过滤器:
<filter>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
想了解这个过滤器的原理可以参考:OpenSessionInViewFilter解决Hibernate延时加载问题
解决方法2:
要注意注解扫描的位置,如果在Controller中也扫描了Service,这个时候的Service是没有事务特性的,所以会报错。
我就是因为这个原因,注解扫描将Controller与Service一起扫描了,应当设置忽略Controller,在spring-mvc.xml中单独扫描Controller。
<context:component-scan base-package="com.ssh.app">
<!--扫描时跳过 @Controller 注解的JAVA类(控制器)-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<context:component-scan base-package="com.ssh.app.web.controller" />
解决方案就是不要在Controller中扫描事务相关的Service。
二、善待补充
未完待续……