Spring 批量 MongoDB 和交易
Spring batch with MongoDB and transactions
我有一个带有两个数据库的 Spring 批处理应用程序:一个 SQL 数据库用于 Spring 批处理元数据,另一个是 Mongo 数据库,其中所有存储业务数据。关系数据库仍然使用DataSourceTransactionManager
。
但是我不认为 Mongo 写入是在具有回滚的活动事务中完成的。这是 MongoItemWriter
上 official Spring Batch documentation 的摘录:
A ItemWriter implementation that writes to a MongoDB store using an implementation of Spring Data's MongoOperations. Since MongoDB is not a transactional store, a best effort is made to persist written data at the last moment, yet still honor job status contracts. No attempt to roll back is made if an error occurs during writing.
然而,情况已不再如此; MongoDB introduced ACID transactions in version 4.
如何将事务添加到我的写入中?当我使用 ItemWriterAdapter
时,我可以在我的服务方法上使用 @Transactional
。但仍然不知道如何处理 MongoItemWriter
...这里正确的配置是什么?谢谢。
I have a Spring Batch application with two databases: one SQL DB for the Spring Batch meta data, and another which is a MongoDB where all the business data is stored.
我邀请您查看以下帖子以了解此设计选择的含义:
- How to java-configure separate datasources for spring batch data and business data? Should I even do it?
在您的例子中,您有一个跨两个数据源的分布式事务:
- SQL 作业存储库的数据源,由
DataSourceTransactionManager
管理
- MongoDB 为您的步骤(使用
MongoItemWriter
),由 MongoTransactionManager
管理
如果你想让技术元数据和业务数据committed/rolled回到同一个分布式事务的范围内,你需要使用一个JtaTransactionManager
协调DataSourceTransactionManager
和 MongoTransactionManager
。您可以在此处找到有关此事的一些资源:.
顺便说一句,在 Spring 批次中有一个使用 MongoDB 作为作业存储库的功能请求:https://github.com/spring-projects/spring-batch/issues/877。实现后,您可以将业务数据和技术元数据存储在同一个数据源中(因此不再需要分布式事务)并且您可以对作业存储库和你的脚步。
我有一个带有两个数据库的 Spring 批处理应用程序:一个 SQL 数据库用于 Spring 批处理元数据,另一个是 Mongo 数据库,其中所有存储业务数据。关系数据库仍然使用DataSourceTransactionManager
。
但是我不认为 Mongo 写入是在具有回滚的活动事务中完成的。这是 MongoItemWriter
上 official Spring Batch documentation 的摘录:
A ItemWriter implementation that writes to a MongoDB store using an implementation of Spring Data's MongoOperations. Since MongoDB is not a transactional store, a best effort is made to persist written data at the last moment, yet still honor job status contracts. No attempt to roll back is made if an error occurs during writing.
然而,情况已不再如此; MongoDB introduced ACID transactions in version 4.
如何将事务添加到我的写入中?当我使用 ItemWriterAdapter
时,我可以在我的服务方法上使用 @Transactional
。但仍然不知道如何处理 MongoItemWriter
...这里正确的配置是什么?谢谢。
I have a Spring Batch application with two databases: one SQL DB for the Spring Batch meta data, and another which is a MongoDB where all the business data is stored.
我邀请您查看以下帖子以了解此设计选择的含义:
- How to java-configure separate datasources for spring batch data and business data? Should I even do it?
在您的例子中,您有一个跨两个数据源的分布式事务:
- SQL 作业存储库的数据源,由
DataSourceTransactionManager
管理
- MongoDB 为您的步骤(使用
MongoItemWriter
),由MongoTransactionManager
管理
如果你想让技术元数据和业务数据committed/rolled回到同一个分布式事务的范围内,你需要使用一个JtaTransactionManager
协调DataSourceTransactionManager
和 MongoTransactionManager
。您可以在此处找到有关此事的一些资源:.
顺便说一句,在 Spring 批次中有一个使用 MongoDB 作为作业存储库的功能请求:https://github.com/spring-projects/spring-batch/issues/877。实现后,您可以将业务数据和技术元数据存储在同一个数据源中(因此不再需要分布式事务)并且您可以对作业存储库和你的脚步。