SpringBoot多线程进行事务管理

首先,各种原理、解决方案,什么使用注解、使用编程式、什么使用阿里巴巴开发的transmittable-thread-localGithub地址 中的TransmittableThreadLocal等等。

百度出来一大度,这里不做阐述,主打一个眼花缭乱,可以自行百度,不过在我看来只有最合适的方法,没有一定最好的方法,这里只是提供一种可用解决方案,以及自己记录罢了,不要杠我。

在Spring管理的类,如service中

scss 复制代码
@Autowired
PlatformTransactionManager transactionManager;

public void getInfoTransaction(int pageIndex) {
    // 创建新的事务
    DefaultTransactionDefinition def = new DefaultTransactionDefinition();
    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    TransactionStatus status = transactionManager.getTransaction(def);
    try {
        // 处理业务
        
        // 事务提交
        transactionManager.commit(status);
    } catch (Exception e) {
        // 事务回滚
        transactionManager.rollback(status);
    }
}

可能这样你觉得这怎么用,还不是每次都要创建

但是

java 复制代码
// ServiceCommon接口 空又或者有getInfoTransaction方法,看自己
public abstract class ServiceCommonImpl implements ServiceCommon{
    @Autowired
    PlatformTransactionManager transactionManager;

    public abstract void getInfo();

    public void getInfoTransaction() {
        // 创建新的事务
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            // 处理业务
            getInfo();
            // 事务提交
            transactionManager.commit(status);
        } catch (Exception e) {
            // 事务回滚
            transactionManager.rollback(status);
        }
    }
}

public class Test extends ServiceCommonImpl{

    @Override
    public void getInfo() {
        // 业务实现
    }

    public void test(){
    // 获取线程池
        ThreadPoolExecutor threadPoolExecutor = SpringUtils.getBean("BfThreadPool");
        threadPoolExecutor.execute(() -> {
            // 使用
            getInfoTransaction();
        });
    }
   
}

你又说,这一个service我就不可能就一个方法用事务,方法改一改嘛

scala 复制代码
public abstract class ServiceCommonImpl implements ServiceCommon{
    @Autowired
    PlatformTransactionManager transactionManager;

     // 方法作为参数传入 
    public void getInfoTransaction(Consumer method) {
        // 创建新的事务
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            // 处理业务
            method.accept(null);
            // 事务提交
            transactionManager.commit(status);
        } catch (Exception e) {
            // 事务回滚
            transactionManager.rollback(status);
        }
    }

}

public class Test extends ServiceCommonImpl {

    public void test() {
        ThreadPoolExecutor threadPoolExecutor = SpringUtils.getBean("BfThreadPool");
        threadPoolExecutor.execute(() -> {
            getInfoTransaction((nullValue) -> {

            });
        });
    }
}

你又说,不好意思,想不到了,哈哈,G

研究其他方法吧,哈哈哈

最后,在非Spring管理类中,通过上下文获取事务管理器的bean,即transactionManager,不是PlatformTransactionManager,这个是个接口,具体实现的时候是动态加载实现类的,然后重复操作,你会发现没用,会报数据源为空的错误,有兴趣的可以试试。

至于原因,查了查,应该是Spring的事务管理器是会自动注入数据源的,你自己直接获取的是没有注入的,大概是这个意思,具体可以看以下感谢的文章。

感谢:

# Spring事务(Transaction)管理高级篇一栈式解决开发中遇到的事务问题

# 【老王读Spring Transaction-4】Spring事务管理的核心原理------PlatformTransactionManager&TransactionStatus

相关推荐
山璞1 分钟前
Flutter3.32 中使用 webview4.13 与 vue3 项目的 h5 页面通信,以及如何调试
前端·flutter
努力早日退休4 分钟前
Antd Image标签父元素会比图片本身高几个像素的原因
前端
林希_Rachel_傻希希4 分钟前
手写Promise--教学版本
前端·javascript·面试
ETA88 分钟前
`console.log([1,2,3].map(parseInt))` 深入理解 JavaScript 中的高阶函数与类型机制
前端·javascript
呼叫69458 分钟前
图片列表滚动掉帧的原因分析与解决方案
前端
狗哥哥11 分钟前
AI 驱动前端自动化测试:一套能落地、能协作、能持续的工程化方案
前端·测试
全栈老石15 分钟前
别再折腾端口转发了:使用 Cloudflare Tunnel 优雅地分享你的 localhost
前端·后端·全栈
码云之上20 分钟前
WEB端小屏切换纯CSS实现
前端·css
LaughingDangZi34 分钟前
vue+java分离项目实现微信公众号开发全流程梳理
java·前端·后端