本文主要是介绍SpringMVC+Hibernate4 导致事务失效不提交的可能原因,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 对于用annotation方式的事务注解和bean配置,spring的配置文件 与springMVC的配置文件对包的重复扫描装配会照成失效在主容器中(applicationContext.xml),将Controller的注解排除掉
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
因为spring的context是父子容器,所以会产生冲突,由ServletContextListener产生的是父容器,springMVC产生的是子容器,子容器Controller进行扫描装配时装配了@Service注解的实例,而该实例理应由父容器进行初始化以保证事务的增强处理,所以此时得到的将是原样的Service(没有经过事务加强处理,故而没有事务处理能力。)
2.Hibernate4中session开启的方式sessionFactory.openSession()导致,应该用getCurrentSession()开启
具体区别请参考
Hibernate4之getCurrentSession和openSession
这篇关于SpringMVC+Hibernate4 导致事务失效不提交的可能原因的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!