spring + springMVC声明式事务失效问题

xiaoxiao2021-02-28  79

事务分为声明式事务和编程式事务,现遇到了spring声明式事务失效了,也在网上查阅了一些别人说的,自己也进行了一些测试。现总结一下。

1.网络:在service层去掉try...catch块,异常要抛出去。经验证,事务还是无效。

2.网络:在service层增加@Transaction注解,经验证,事务仍是无效的。

3.写main方法测试,用ApplicationContext读取spring配置文件获取bean

private static ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-***.xml") ;

context获取bean,用这样方式测试事务的时候是没有问题的。

<context:component-scan base-package="com.ada.wuliu">         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>      </context:component-scan>

修改springmvc的配置文件,不加载service,只让spring的容器去加载servcie,网上说spring的容器加载能得到事务的增强。启动后测试事务果然没有问题了。

问题解决。根本的原因还待研究。

在网上查找时有两篇博客写的不错

其中一个:http://blog.csdn.net/z69183787/article/details/37819627

另一个分析了源码,找不到了。。。。。

转载请注明原文地址: https://www.6miu.com/read-66137.html

最新回复(0)