欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分析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>

然后寫(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)文章

最新評(píng)論