最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

分析Springboot中嵌套事務(wù)失效原因詳解

 更新時(shí)間:2021年11月15日 11:44:54   作者:秋天的春  
這篇文章主要為大家介紹了分析Springboot中嵌套事務(wù)失效原因詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步

首先兩個(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>

然后寫個(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ù),說明事務(wù)失效了

runtimeException

數(shù)據(jù)庫(kù)兩條數(shù)據(jù)都上傳了,說明事務(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");
    }

回到上面的問題,會(huì)發(fā)現(xiàn)trance1()沒有走代理,所以會(huì)出現(xiàn)兩個(gè)都插入數(shù)據(jù)庫(kù)的操作。
那么需要得到當(dāng)前的代理對(duì)象,然后調(diào)用trance1()
通過AopContext.currentProxy()獲得當(dāng)前代理

 ((UserService)AopContext.currentProxy()).trance1();

改成這樣調(diào)用trance1()
運(yùn)行Test,然后數(shù)據(jù)庫(kù)就剩一條數(shù)據(jù)了,說明trance1()方法回滾了。

以上就是分析Springboot中嵌套事務(wù)失效原因詳解的詳細(xì)內(nèi)容,更多關(guān)于Springboot中嵌套事務(wù)失效分析的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

合山市| 常熟市| 本溪| 集贤县| 承德县| 海口市| 武清区| 罗山县| 彭水| 神农架林区| 静海县| 镇远县| 遂宁市| 策勒县| 静安区| 新干县| 赤水市| 子长县| 阿拉善右旗| 黔东| 睢宁县| 盐边县| 金乡县| 江安县| 古丈县| 泸定县| 阳春市| 石台县| 德惠市| 洪雅县| 谢通门县| 娄烦县| 巫山县| 友谊县| 沾益县| 永泰县| 从江县| 灵丘县| 綦江县| 西宁市| 潢川县|