欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

致命问题多数据源引发springbatch多事物 -凯发k8官方网

发布时间:2025/1/21 javascript 18 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 致命问题多数据源引发springbatch多事物 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

      • 一、如何查看springbatch原数据和业务数据是一个事物
      • 二、多数据源如何引发springbatch多事物

写在前面: 我是「境里婆娑」。我还是从前那个少年,没有一丝丝改变,时间只不过是考验,种在心中信念丝毫未减,眼前这个少年,还是最初那张脸,面前再多艰险不退却。
写博客的目的就是分享给大家一起学习交流,如果您对 java感兴趣,可以关注我,我们一起学习。

前言:我们项目在运行的时候出现了多事物问题,springbatch原数据表提交和业务表数据提交用的不是一个事物,这在项目运行中是一个非常致命的问题。在极端情况下断点续跑可能会出现丢失数据,所以解决这个问题迫在眉睫。

一、如何查看springbatch原数据和业务数据是一个事物

springbatch源代码taskletstep这个类的dointransaction这个方法时保存业务数据和springbatch源数据表。

  • 保存业务数据
    result = tasklet.execute(contribution, chunkcontext);

  • 保存springbatch上下文表
    getjobrepository().updateexecutioncontext(stepexecution);

  • 保存springbatch step表
    getjobrepository().update(stepexecution);

为了验证保存业务数据和保存springbatch源数据是使用一个事物提交,我们可以通过debug方式分别把断点打到这三行代码上。

result = tasklet.execute(contribution, chunkcontext); getjobrepository().updateexecutioncontext(stepexecution); getjobrepository().update(stepexecution);

查询springbatch业务表和源数据表断点走完业务表无数据、batch上下文表,batch_step_execution表都是最初是状态则说明是一个事物。

select * from batch_step_execution order by step_execution_id desc; select * from batch_step_execution_context order by step_execution_id desc; select * from cat;

batch_step_execution_context初始状态

159 {"batch.tasklettype":"org.springframework.batch.core.step.item.chunkorientedtasklet","flatfileitemreader.read.count":0,"batch.steptype":"org.springframework.batch.core.step.tasklet.taskletstep"}

batch_step_execution初始状态

187 3 cafecatstep 1015 2020-11-25 10:02:05 2020-11-25 10:03:03 starting 0 0 0 0 0 0 0 0 unknown 2020-11-25 10:03:03

springbatch源码taskletstep类dointransaction方法

public repeatstatus dointransaction(transactionstatus status) {transactionsynchronizationmanager.registersynchronization(this);repeatstatus result = repeatstatus.continuable;//代码省略.......result = tasklet.execute(contribution, chunkcontext);//代码省略...stepexecutionupdated = true;getjobrepository().updateexecutioncontext(stepexecution);getjobrepository().update(stepexecution);//代码省略...return result;}

二、多数据源如何引发springbatch多事物

项目中使用了多数据源,而且重写了basicbatchconfigurer这个类的这个initialize方法。而且这个类中重新new了一个新的数据源,通过各种排查确定出原因是这个new的新数据源导致多事物问题。

详细代码如下:

总结:我们最好不去修改原生态的springbatch代码,否则会出现意想不到的结果。

总结

以上是凯发k8官方网为你收集整理的致命问题多数据源引发springbatch多事物的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图