當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
云笔记项目-Spring事务学习-传播MANDATORY
生活随笔
收集整理的這篇文章主要介紹了
云笔记项目-Spring事务学习-传播MANDATORY
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接下來測試事務傳播屬性MANDATORY
Service層
所有Service層實現類都設置事務傳播屬性為MANDATORY。
LayerT層代碼
1 package LayerT; 2 3 import javax.annotation.Resource; 4 5 import org.springframework.stereotype.Component; 6 import org.springframework.transaction.annotation.Transactional; 7 8 import Entity.EMP; 9 import Service.EMPService1; 10 import Service.EMPService2; 11 12 /** 13 * 測試Mandatory 14 * @author yangchaolin 15 * 16 */ 17 @Component("mandatoryTest") 18 public class MandatoryTest { 19 @Resource(name="service1") 20 EMPService1 service1; 21 @Resource(name="service2") 22 EMPService2 service2; 23 /** 24 * 外層方法沒有事務,但是拋出異常 25 * @param emp1 26 * @param emp2 27 */ 28 public void testMandatoryWithoutTransaction1(EMP emp1,EMP emp2) { 29 service1.addEmp1(emp1); 30 service2.addEmp2(emp2); 31 throw new RuntimeException(); 32 } 33 /** 34 * 外層方法沒有事務,內層方法拋出異常 35 * @param emp1 36 * @param emp2 37 */ 38 public void testMandatoryWithoutTransaction2(EMP emp1,EMP emp2) { 39 service1.addEmp1(emp1); 40 service2.addEmp2WithException(emp2); 41 } 42 /** 43 * 外層方法有事務,并且拋出異常 44 * @param emp1 45 * @param emp2 46 */ 47 @Transactional 48 public void testMandatoryWithTransaction1(EMP emp1,EMP emp2) { 49 service1.addEmp1(emp1); 50 service2.addEmp2(emp2); 51 throw new RuntimeException(); 52 } 53 /** 54 * 外層方法有事務,內層方法拋出異常 55 * @param emp1 56 * @param emp2 57 */ 58 @Transactional 59 public void testMandatoryWithTransaction2(EMP emp1,EMP emp2) { 60 service1.addEmp1(emp1); 61 service2.addEmp2WithException(emp2); 62 } 63 /** 64 * 外層方法有事務,內層方法拋出異常被捕獲 65 * @param emp1 66 * @param emp2 67 */ 68 @Transactional 69 public void testMandatoryWithTransaction3(EMP emp1,EMP emp2) { 70 service1.addEmp1(emp1); 71 try { 72 service2.addEmp2WithException(emp2); 73 }catch(Exception e) { 74 System.out.println("回滾"); 75 } 76 } 77 /** 78 * 外層方法有事務,沒有異常發生 79 * @param emp1 80 * @param emp2 81 */ 82 @Transactional 83 public void testMandatoryWithTransaction4(EMP emp1,EMP emp2) { 84 service1.addEmp1(emp1); 85 service2.addEmp2(emp2); 86 } 87 }測試代碼
1 package TestCase; 2 3 import org.junit.Test; 4 5 import Entity.EMP; 6 import LayerT.MandatoryTest; 7 8 public class mandatoryTestCase extends baseTest{ 9 @Test 10 public void test1() { 11 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 12 EMP emp1=new EMP("張三",18); 13 EMP emp2=new EMP("李四",28); 14 T1.testMandatoryWithoutTransaction1(emp1, emp2); 15 } 16 @Test 17 public void test2() { 18 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 19 EMP emp1=new EMP("張三",18); 20 EMP emp2=new EMP("李四",28); 21 T1.testMandatoryWithoutTransaction2(emp1, emp2); 22 } 23 @Test 24 public void test3() { 25 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 26 EMP emp1=new EMP("張三",18); 27 EMP emp2=new EMP("李四",28); 28 T1.testMandatoryWithTransaction1(emp1, emp2); 29 } 30 @Test 31 public void test4() { 32 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 33 EMP emp1=new EMP("張三",18); 34 EMP emp2=new EMP("李四",28); 35 T1.testMandatoryWithTransaction2(emp1, emp2); 36 } 37 @Test 38 public void test5() { 39 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 40 EMP emp1=new EMP("張三",18); 41 EMP emp2=new EMP("李四",28); 42 T1.testMandatoryWithTransaction3(emp1, emp2); 43 } 44 @Test 45 public void test6() { 46 MandatoryTest T1=ac.getBean("mandatoryTest",MandatoryTest.class); 47 EMP emp1=new EMP("張三",18); 48 EMP emp2=new EMP("李四",28); 49 T1.testMandatoryWithTransaction4(emp1, emp2); 50 } 51 }測試結果
(1)外層方法沒有事務
| test1 | 張三未插入,李四未插入 |
| test2 | 張三未插入,李四未插入 |
?
?
?
測試報錯內容為:"No existing transaction found for transaction marked with propagation 'mandatory' "。
結論:外層方法沒有事務,內層方法事務傳播屬性為MANDATROY時,將無法插入,并執行報上述錯誤,提示找到不到已有事務,即找不到外層方法中的事務。
(2)外層方法有默認事務屬性
| test3 | 張三未插入,李四未插入 |
| test4 | 張三未插入,李四未插入 |
| test5 | 張三未插入,李四未插入 |
| test6 | 張三插入,李四插入 |
?
?
?
?
?
結論:只有外層方法有事務的情況下,才能執行內層聲明事務傳播屬性為MANDATORY的方法,否則將報錯。當外層方法添加事務后,內層或者外層方法隨便一個出異常,都會導致整體事務回滾,只有都沒有異常時才能正常插入數據。
?
參考博文:https://segmentfault.com/a/1190000013341344
轉載于:https://www.cnblogs.com/youngchaolin/p/10629795.html
總結
以上是生活随笔為你收集整理的云笔记项目-Spring事务学习-传播MANDATORY的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日一题#02
- 下一篇: Spring Boot 面试,一个问题就