spring 事务管理——回滚之service层(事务控制层)代码互调

2023-12-08 05:58

本文主要是介绍spring 事务管理——回滚之service层(事务控制层)代码互调,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

spring事务管理相关的文章已经有很多了,本人写此文章主要为自己的实验做一个记录,年纪大了,记性不好偷笑

首先先贴几个地址,有兴趣研读的同学可以参考一下:

初级使用:

http://blog.csdn.net/xugangjava/article/details/6770799

初级容易犯的错:事务中catch异常

http://blog.csdn.net/yipanbo/article/details/46048413

官方介绍:

http://docs.spring.io/spring/docs/2.0.x/reference/transaction.html

默认回滚配置实验:

http://blog.csdn.net/lovejavaydj/article/details/7635848


以上几个地址是从不同的角度来讲spring的事务处理的,本文并非重复的去做以上文中已做过的实验,本文的实验对象是两个事务方法之间的调用,检验是否产生回滚。

实验准备:

1、采用spring声明式事务,实现方式,spring的xml文件中进行配置,配置核心代码如下:

[html]  view plain copy
  1. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  2.         <property name="dataSource" ref="zigbeeDataSource" />  
  3.     </bean>  
  4.       
  5.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  6.         <tx:attributes>  
  7.             <tx:method name="get*" read-only="true"  />  
  8.             <tx:method name="query*" read-only="true"  />  
  9.             <tx:method name="find*" read-only="true" />  
  10.             <tx:method name="is*" read-only="true" />  
  11.             <tx:method name="*" rollback-for="Exception"/><!--默认回滚机制是RuntimeException-->  
  12.         </tx:attributes>  
  13.     </tx:advice>      
  14.      
  15.     <aop:config>  
  16.         <aop:pointcut id="service" expression="execution(* com.my.test..*Service*.*(..))" /><!--声明所有包含Service的类的所有方法使用事务-->  
  17.         <aop:advisor advice-ref="txAdvice" pointcut-ref="service" />  
  18.     </aop:config>  
2、测试类:接口,TestService,方法test() , test1();测试类 TestMain,main方法(此处只是简要写明测试核心代码,真正测试方法中需要初始化spring),代码如下:

[java]  view plain copy
  1. public interface TestService {  
  2.         void test()throws Exception;  
  3.     void test1()throws Exception;  
  4. }  

[java]  view plain copy
  1. //本文验证的代码,一下对两个方法的不同实现方式进行验证说明  
  2. @Service(value="testService")  
  3. public class TestServiceImpl  implements TestService {  
  4.     @Autowired  
  5.     TestDao testDao;  
  6.     @Override  
  7.     public void test() throws Exception {  
  8.         // TODO  
  9.     }  
  10.     @Override  
  11.     public void test1() throws Exception {  
  12.         // TODO  
  13.     }  
  14. }  

 
[java]  view plain copy
  1. public class TestMain {  
  2.         public static void main(String[] args) {  
  3.         testService.test1();  
  4.     }  
  5. }  
 
3、测试方案: 

①根据配置文件txAdvice所示,test和test1都应有事务支持,当分别单独调用两个方法时,遇到异常抛出时,都应回滚。如TestMain中的main方法调用,在TestService中实现两个方法如下两种情况,事务都会回滚,PS:作者做过更多实验,将异常抛出的位置任意变换,都会进行回滚,数据库不会插入任何数据。

(结论:当两个都有事务回滚的方法之间进行相互调用,无论哪个方法中有异常,在任何位置进行异常抛出,两个方法执行的数据都会回滚)

[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         throw new Exception(); // 此处抛出异常  
  13.     }  
  14.     @Override  
  15.     public void test1() throws Exception {  
  16.         TestEntity te = new TestEntity();  
  17.         te.setId(2);  
  18.         te.setName("222");  
  19.         testDao.insert(te);  
  20.           
  21.         this.test(); // 调用test()方法,在test方法中抛出异常  
  22.           
  23.     }  
  24. }  
[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.     }  
  12.     @Override  
  13.     public void test1() throws Exception {  
  14.         TestEntity te = new TestEntity();  
  15.         te.setId(2);  
  16.         te.setName("222");  
  17.         testDao.insert(te);  
  18.           
  19.         this.test(); // 调用test()方法,test方法执行成功  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  

②我们修改一些配置文件中的txAdvice, 把<tx:method name="*" rollback-for="Exception"/>改为默认值

[html]  view plain copy
  1. <tx:method name="*"/>  
上述测试代码中的事务均不回滚,充分证明了 spring默认事务回滚机制为RuntimeException。

③我们继续修改配置文件中的txAdvice,继续使用<tx:method name="*" rollback-for="Exception"/>,另外在这一行上面增加一行

[html]  view plain copy
  1. <tx:method name="test1*" no-rollback-for="Exception"/></span>  
我们让test1方法失去事务回滚的控制

继续测试,test方法虽然还在事务控制之中,但是test1没有事务回滚机制,在test方法中抛出异常并不会使事务回滚,数据库会插入两条数据。另外一种验证,test方法执行成功,test1方法抛出异常仍然不会事务回滚,数据库插入两条数据。

而如果我们将main方法中的调用,改为test()的时候,事务有效,会回滚,不插入数据。

[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.         throw new Exception(); // 此处抛出异常  
  12.     }  
  13.     @Override  
  14.     public void test1() throws Exception {  
  15.         TestEntity te = new TestEntity();  
  16.         te.setId(2);  
  17.         te.setName("222");  
  18.         testDao.insert(te);  
  19.           
  20.         this.test(); // 调用test()方法,test方法中抛出异常  
  21.     }  
  22. }  
[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.     }  
  12.     @Override  
  13.     public void test1() throws Exception {  
  14.         TestEntity te = new TestEntity();  
  15.         te.setId(2);  
  16.         te.setName("222");  
  17.         testDao.insert(te);  
  18.           
  19.         this.test(); // 调用test()方法,test方法执行成功  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  

④延续③中的配置文件,我们再换一种测试,main方法中调用test,TestServiceImpl中,test调用test1, 因为test有事务回滚支持,因此无论在哪个方法中抛出异常,事务都会回滚。

[java]  view plain copy
  1. public class TestMain {  
  2.         public static void main(String[] args) {  
  3.         testService.test();  
  4.     }  
  5. }  
[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         this.test1(); // 调用test1()方法,在test1方法中抛出异常  
  13.     }  
  14.     @Override  
  15.     public void test1() throws Exception {  
  16.         TestEntity te = new TestEntity();  
  17.         te.setId(2);  
  18.         te.setName("222");  
  19.         testDao.insert(te);  
  20.           
  21.         throw new Exception(); // 此处抛出异常  
  22.     }  
  23. }  
[java]  view plain copy
  1. @Service(value="testService")  
  2. public class TestServiceImpl  implements TestService {  
  3.     @Autowired  
  4.     TestDao testDao;  
  5.     @Override  
  6.     public void test() throws Exception {  
  7.         TestEntity te = new TestEntity();  
  8.         te.setId(1);  
  9.         te.setName("111");  
  10.         testDao.insert(te);  
  11.           
  12.         this.test1(); // 调用test1()方法,test1方法执行成功  
  13.           
  14.         throw new Exception(); // 此处抛出异常  
  15.     }  
  16.     @Override  
  17.     public void test1() throws Exception {  
  18.         TestEntity te = new TestEntity();  
  19.         te.setId(2);  
  20.         te.setName("222");  
  21.         testDao.insert(te);  
  22.     }  
  23. }  

至此,本文的验证完毕,限于本人水平有限,文章难免有遗漏和不足支持,欢迎各位读者给予指导更正批评!

另外,对于文章中的代码,只是截取代码片段,如有错误请自行修改,并在回复中给予指出,切勿原文照搬测试。

这篇关于spring 事务管理——回滚之service层(事务控制层)代码互调的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/468752

相关文章

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

javacv依赖太大导致jar包也大的解决办法

《javacv依赖太大导致jar包也大的解决办法》随着项目的复杂度和依赖关系的增加,打包后的JAR包可能会变得很大,:本文主要介绍javacv依赖太大导致jar包也大的解决办法,文中通过代码介绍的... 目录前言1.检查依赖2.更改依赖3.检查副依赖总结 前言最近在写项目时,用到了Javacv里的获取视频

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima