【问题标题】:Facing issue with commits when using 2 datasources in Spring Batch在 Spring Batch 中使用 2 个数据源时面临提交问题
【发布时间】:2019-10-23 22:01:36
【问题描述】:

我的项目进行批处理,并尝试使用 2 个数据源 (oracle)(一个用于 Spring 元数据,第二个用于应用程序)。

示例配置位于 github 项目 (github.com/jobas2007/spring_proj) 中,但由于配置了 2 个事务管理器/数据源,因此在为 2 个数据源完成的提交之间管理事务似乎不一致。主数据源(用于批处理)提交正确发生,但最终在 ItemWriter 中执行“保存”时,在保存/更新应用程序表期间观察到非常不可预测的行为。

请提供一种管理多个数据源之间事务的好方法?

根据链接https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/,进入步骤时Tx开始一个块

尝试了其中一篇文章提出的使用“ChainedTransactionManager”的建议,但在布线方面面临重大问题 Use of multiple DataSources in Spring Batch

代码在github

预期结果是拥有可预测的稳健 tx 管理

【问题讨论】:

  • 分布式事务确实是管理这个问题的唯一方法。
  • 能否请您提供一些代码sn-p或说明,如何在spring批处理中启用分布式tx(主要用于spring元数据)和应用程序的辅助数据源?对于应用级提交,可以在@Service类中注入“Global/Chained Tx Manager”(在tx边界内调用repository.save()),但是如何在Spring开始时通过Golbal/Chained Tx manager注入批处理步骤(其中 tx 根据此链接 blog.codecentric.de/en/2012/03/…)?
  • 有人还尝试使用“atomikos”进行分布式交易,但似乎仍有问题 - stackoverflow.com/questions/38323207/… ...请协助?

标签: spring-boot spring-batch


【解决方案1】:

如果您决定在 Spring Batch 中使用两个数据源,则需要使用 JtaTransactionManager 在两个数据源之间同步 distributed transaction

您可以在此处找到 David Syer 博士关于此问题的精彩文章:Distributed transactions in Spring, with and without XA

【讨论】:

  • 确实按照文章并通过使用链式 tx mgr 做了类似的事情,但又如何在“步骤”的开头注入它? ----------// 跨 2 个资源/数据源的单个 tx 上下文 @Bean(name = "globalTransactionManager") public ChainedTransactionManager build(@Qualifier("batchConfigurer") BatchConfigurer 配置器,@Qualifier("transactionManager" ) PlatformTransactionManager appTransactionManager) 抛出异常 { ChainedTransactionManager ctm = new ChainedTransactionManager(configurer.getTransactionManager(), appTransactionManager);返回ctm; }
猜你喜欢
  • 1970-01-01
  • 2017-07-23
  • 2015-08-12
  • 2020-11-03
  • 2014-10-21
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2021-02-25
相关资源
最近更新 更多