javascript
云笔记项目-Spring事务学习-传播MANDATORY
接下來測試事務(wù)傳播屬性MANDATORY
Service層
所有Service層實(shí)現(xiàn)類都設(shè)置事務(wù)傳播屬性為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 * 外層方法沒有事務(wù),但是拋出異常 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 * 外層方法沒有事務(wù),內(nèi)層方法拋出異常 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 * 外層方法有事務(wù),并且拋出異常 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 * 外層方法有事務(wù),內(nèi)層方法拋出異常 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 * 外層方法有事務(wù),內(nèi)層方法拋出異常被捕獲 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 * 外層方法有事務(wù),沒有異常發(fā)生 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 }測試結(jié)果
(1)外層方法沒有事務(wù)
| test1 | 張三未插入,李四未插入 |
| test2 | 張三未插入,李四未插入 |
?
?
?
測試報錯內(nèi)容為:"No existing transaction found for transaction marked with propagation 'mandatory' "。
結(jié)論:外層方法沒有事務(wù),內(nèi)層方法事務(wù)傳播屬性為MANDATROY時,將無法插入,并執(zhí)行報上述錯誤,提示找到不到已有事務(wù),即找不到外層方法中的事務(wù)。
(2)外層方法有默認(rèn)事務(wù)屬性
| test3 | 張三未插入,李四未插入 |
| test4 | 張三未插入,李四未插入 |
| test5 | 張三未插入,李四未插入 |
| test6 | 張三插入,李四插入 |
?
?
?
?
?
結(jié)論:只有外層方法有事務(wù)的情況下,才能執(zhí)行內(nèi)層聲明事務(wù)傳播屬性為MANDATORY的方法,否則將報錯。當(dāng)外層方法添加事務(wù)后,內(nèi)層或者外層方法隨便一個出異常,都會導(dǎo)致整體事務(wù)回滾,只有都沒有異常時才能正常插入數(shù)據(jù)。
?
參考博文:https://segmentfault.com/a/1190000013341344
轉(zhuǎn)載于:https://www.cnblogs.com/youngchaolin/p/10629795.html
總結(jié)
以上是生活随笔為你收集整理的云笔记项目-Spring事务学习-传播MANDATORY的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日一题#02
- 下一篇: linux进程篇 (二) 进程的基本控制