I have 2 different datasources, one to read and another one to write results like below:
knowing that reader and writer are in the same tasklet.
As per the documentation, we can configure a single transaction manager at tasklet
In this scenario, how do i use the transaction manager here?
I cannot rely on the container and i'm not using ORM layer (JPA..), i use direct JDBC driver to read in database 1 and write into 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 could i configure the JTA/XA transaction ( Atomikos ) with Spring Batch?
With the xml one you have to be explicit in which datasource Spring Batch uses. If you don't declare it explicitly with Java based configuration it will try to detect the datasource to work, which will only work in case a single datasource is detected. YOu could try annotating the one to use for Batch with @Primary .
Spring Boot provides first-class support to the Spring JPA that makes it easy to access the database with little boilerplate code by using Spring Repositories feature. Spring Boot does not provide an out of the box solution in case our application needs multiple DataSources (e.g. multi-tenant system).
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With