栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

@Transactional注解Spring Boot 事务方法调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@Transactional注解Spring Boot 事务方法调用

@Transactional注解 事务方法调用

总结,
事务方法调用非事务方法-生效
非事务方法调用事务方法-不生效

事务方法调用非事务方法(相同类下)

import edu.haut.springboottest.entity.TestTable;
import edu.haut.springboottest.mapper.TestTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
 
@Service
public class TestTableService {
 
    @Resource
    private TestTableMapper testTableMapper;
 
    @Transactional
    public void add() {
        add1();
        add2();
        add3();
    }
 

    public void add1() {
        testTableMapper.selectById(1);
    }
 

    public void add2() {
        add4();
    }
 

    public void add3() {
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
    }
 

    public void add4() {
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
    }
}

结果:插入成功,事务生效

非事务方法使用事务方法(同类内)

import edu.haut.springboottest.entity.TestTable;
import edu.haut.springboottest.mapper.TestTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
 
@Service
public class TestTableService {
 
    @Resource
    private TestTableMapper testTableMapper;
 

    public void add() {
        add1();
        add2();
        add3();
    }
 
    @Transactional
    public void add1() {
        testTableMapper.selectById(1);
    }
 
    @Transactional
    public void add2() {
        add4();
    }
 
    @Transactional
    public void add3() {
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
    }
 
    @Transactional
    public void add4() {
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
        testTableMapper.insert(new TestTable(1, "ceshi", (byte) 1));
    }
}

不论多深层的方法,都插入了一条数据。事务控制失败

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584185.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号