日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

云笔记项目-Spring事务学习-传播MANDATORY

發(fā)布時(shí)間:2025/4/14 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 云笔记项目-Spring事务学习-传播MANDATORY 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

接下來測試事務(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張三未插入,李四未插入

?

?

?

測試報(bào)錯(cuò)內(nèi)容為:"No existing transaction found for transaction marked with propagation 'mandatory' "。

結(jié)論:外層方法沒有事務(wù),內(nèi)層方法事務(wù)傳播屬性為MANDATROY時(shí),將無法插入,并執(zhí)行報(bào)上述錯(cuò)誤,提示找到不到已有事務(wù),即找不到外層方法中的事務(wù)。

(2)外層方法有默認(rèn)事務(wù)屬性

test3張三未插入,李四未插入
test4張三未插入,李四未插入
test5張三未插入,李四未插入
test6張三插入,李四插入

?

?

?

?

?

結(jié)論:只有外層方法有事務(wù)的情況下,才能執(zhí)行內(nèi)層聲明事務(wù)傳播屬性為MANDATORY的方法,否則將報(bào)錯(cuò)。當(dāng)外層方法添加事務(wù)后,內(nèi)層或者外層方法隨便一個(gè)出異常,都會(huì)導(dǎo)致整體事務(wù)回滾,只有都沒有異常時(shí)才能正常插入數(shù)據(jù)。

?

參考博文:https://segmentfault.com/a/1190000013341344

轉(zhuǎn)載于:https://www.cnblogs.com/youngchaolin/p/10629795.html

總結(jié)

以上是生活随笔為你收集整理的云笔记项目-Spring事务学习-传播MANDATORY的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。