分析Springboot中嵌套事務(wù)失效原因詳解
首先兩個(gè)事務(wù)方法,其中一個(gè)調(diào)用另一個(gè)。
@Transactional(rollbackFor = Exception.class) public void trance() { try { trance1();//調(diào)用下一個(gè)事務(wù)方法。 } catch (Exception e) { e.printStackTrace(); } User user = new User(); ShardingIDConfig shardingIDConfig = new ShardingIDConfig(); user.setId(shardingIDConfig.generateKey().longValue()); user.setName("trance"); user.setSex(0); userMapper.create(user); } @Transactional(propagation = Propagation.REQUIRED) public void trance1() throws Exception{ User user = new User(); ShardingIDConfig shardingIDConfig = new ShardingIDConfig(); user.setId(shardingIDConfig.generateKey().longValue()); user.setName("trance1"); user.setSex(1); userMapper.create(user); System.out.println(user.getId()); throw new RuntimeException(); }
添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
然后寫(xiě)個(gè)測(cè)試類,我也是第一次用這個(gè)測(cè)試
import com.lijia.App; import com.lijia.service.UserService; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class Test { @Autowired private UserService userService; @org.junit.Test public void trance(){ userService.trance(); } }
執(zhí)行會(huì)發(fā)現(xiàn)報(bào)了RuntimeException,但是數(shù)據(jù)庫(kù)里面有兩條數(shù)據(jù),說(shuō)明事務(wù)失效了
runtimeException
數(shù)據(jù)庫(kù)兩條數(shù)據(jù)都上傳了,說(shuō)明事務(wù)失效
為什么會(huì)出現(xiàn)這種情況呢
spring事務(wù)使用了動(dòng)態(tài)代理。還是從動(dòng)態(tài)代理去看。
給出一段代碼
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; interface IHello { public void test(); public void test1(); } class Hello implements IHello{ @Override public void test() { System.out.println("test"); } @Override public void test1() { System.out.println("test1"); } } public class MyInvoke implements InvocationHandler{ public Object target; public MyInvoke(Object target){ this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().contains("test")){ System.out.println("========代理了======="); } return method.invoke(target,args); } public static void main(String[] args) { MyInvoke myInvoke = new MyInvoke(new Hello()); IHello iHello = (IHello) Proxy.newProxyInstance(MyInvoke.class.getClassLoader(),new Class[]{IHello.class},myInvoke); iHello.test(); iHello.test1(); } }
將上面的Hello類
中的test1方法
放入test方法
中
public void test() { test1(); System.out.println("test"); }
回到上面的問(wèn)題,會(huì)發(fā)現(xiàn)trance1()
沒(méi)有走代理,所以會(huì)出現(xiàn)兩個(gè)都插入數(shù)據(jù)庫(kù)的操作。
那么需要得到當(dāng)前的代理對(duì)象,然后調(diào)用trance1()
通過(guò)AopContext.currentProxy()
獲得當(dāng)前代理
((UserService)AopContext.currentProxy()).trance1();
改成這樣調(diào)用trance1()
運(yùn)行Test,然后數(shù)據(jù)庫(kù)就剩一條數(shù)據(jù)了,說(shuō)明trance1()
方法回滾了。
以上就是分析Springboot中嵌套事務(wù)失效原因詳解的詳細(xì)內(nèi)容,更多關(guān)于Springboot中嵌套事務(wù)失效分析的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springBoot整合shiro如何解決讀取不到@value值問(wèn)題
這篇文章主要介紹了springBoot整合shiro如何解決讀取不到@value值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-08-08Java語(yǔ)言Iterator轉(zhuǎn)換成 List的方法
在 Java 中,迭代器(Iterator)是一種用于遍歷集合中元素的對(duì)象,它提供了一種簡(jiǎn)單而一致的方式來(lái)訪問(wèn)集合中的元素,而不需要暴露集合內(nèi)部的結(jié)構(gòu),這篇文章主要介紹了Java語(yǔ)言Iterator轉(zhuǎn)換成 List的方法,需要的朋友可以參考下2023-08-08Struts2 的國(guó)際化實(shí)現(xiàn)方式示例
這篇文章主要介紹了Struts2 的國(guó)際化實(shí)現(xiàn)方式示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project報(bào)錯(cuò)問(wèn)題的解決)
這篇文章主要介紹了IDEA配置maven環(huán)境的詳細(xì)教程(Unable to import maven project問(wèn)題的解決),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Java面試題之HashMap 的 hash 方法原理是什么
那天,小二去蔚來(lái)面試,面試官老王一上來(lái)就問(wèn)他:HashMap 的 hash 方法的原理是什么?當(dāng)時(shí)就把裸面的小二給蚌埠住了,這篇文章將詳細(xì)解答該題目2021-11-11idea install 時(shí)提示jdk的某個(gè)jar包的包不存在的問(wèn)題
這篇文章主要介紹了idea install 時(shí)提示jdk的某個(gè)jar包的包不存在的問(wèn)題,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Spring bean對(duì)象實(shí)例化實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Spring bean對(duì)象實(shí)例化實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07SpringBoot2零基礎(chǔ)到精通之profile功能與自定義starter
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開(kāi)發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)profile功能與自定義starter2022-03-03java使用Nagao算法實(shí)現(xiàn)新詞發(fā)現(xiàn)、熱門(mén)詞的挖掘
這篇文章主要介紹了java使用Nagao算法實(shí)現(xiàn)新詞發(fā)現(xiàn)、熱門(mén)詞的挖掘的思路和詳細(xì)代碼,需要的朋友可以參考下2015-07-07