Using two different data sources: Spring package

I have 2 different data sources , one for reading and another for writing the results, as shown below:

  • ItemReader should receive data from dataSource_1.
  • ItemWriter should write data to dataSource_2.

knowing that the reader and the writer are in the same task.

According to the documentation, we can configure one transaction manager in the tasklet

In this scenario, how can I use the transaction manager here?

I cannot rely on the container and I do not use the ORM level (JPA ..), I use the direct JDBC driver to read in database 1 and write to database2.

current conf:

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${batch.or.jdbc.driver}" /> <property name="url" value="${batch.or.jdbc.url}" /> <property name="username" value="${batch.or.jdbc.user}" /> <property name="password" value="${batch.or.jdbc.password}" /> </bean> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${batch.caux.jdbc.driver}" /> <property name="url" value="${batch.caux.jdbc.url}" /> <property name="username" value="${batch.caux.jdbc.user}" /> <property name="password" value="${batch.caux.jdbc.password}" /> </bean> <bean id="baseReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"> <property name="dataSource" ref="dataSource1" /> </bean> <bean id="baseWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter"> <property name="dataSource2" ref="dataSource2" /> <property name="sql" value="${batch.param.insert}" /> </bean> 

How do I set up a JTA / XA transaction (Atomikos) using Spring Batch?

+6
source share
3 answers

Forget the Spring Package.

Suppose you need to write an application service that is read from a single data source1 and write to a data source2, what do you need to do?

Distributed transaction is the answer for you. This, of course, is due to additional configurations. If you are in a J2EE container (Websphere, Weblogic, JBoss, etc.), then the transaction manager that they provide should be able to handle distributed transactions. You should find the appropriate Spring TransactionManager implementation for each of these platforms. You need to configure only the data source (which should also be in the container) to use drivers that support XA, and in your application using the appropriate transaction manager and search for data sources that support XA using JNDI

However, if you cannot rely on a container (for example, you are writing a standalone application, etc.), you will need to find a transaction manager that is capable of this. Atomikos is one of the most famous free JTA / XA libraries.

Of course, if you are one hundred percent sure that all transactional actions will be in datasource2, you can consider using the data transaction manager only for datasource2, but to be honest, this is not the preferred approach that I propose.

0
source

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

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

considers

+2
source

If the reader may be outside the transaction, you can only use the trx writer manager. If you need a reader and writer in the same transaction, you might need an XA compatible transaction manager.

+1
source

Source: https://habr.com/ru/post/946092/


All Articles