Using 2 different datasources : Spring batch

南楼画角 提交于 2019-12-03 07:34:46

Forget about Spring Batch.

Assume you need to write a application service, which read from one datasource1, and write to datasource2, what will you need to do?

Distributed transaction is the answer for you. That of course involve extra configurations. If you are in a J2EE container (Websphere, Weblogic, JBoss etc), then the transaction manager they provide should be able to handle distributed transaction. You should be able to find the corresponding TransactionManager implementation in Spring for each of these platforms. What you need is only configure the datasource (which should be also under the container) to use XA-aware drivers, and in your app, use the corresponding transaction manager, and lookup the XA-aware datasources by JNDI

However if you cannot rely on the container (e.g. you are writing a standalone app, and etc), you will need to find an transaction manager that is capable on that. Atomikos is one the the most famous free JTA/XA library.

Of course, if you are hundred percent sure that all transaction-aware actions will be in datasource2, you can consider using datasource transaction manager only for datasource2, but honestly it is not a preferred approach I will suggest.

You will need to use a XA compatible driver for your 2 data-sources with a JTA transaction Manager.

see this article and this one if you are not familiar with distributed transactions

regards

If the reader can be outside of the transaction, you can use the writer's trx manager only. If you need the reader and the writer in the same transaction, probably you need a XA compatible transaction manager.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!