Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 2 different datasources : Spring batch

I have 2 different datasources, one to read and another one to write results like below:

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

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?

like image 218
Yassine EL AYACHI Avatar asked May 29 '13 14:05

Yassine EL AYACHI


People also ask

How do I use multiple datasource in 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 .

Can we use multiple datasource in spring boot?

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).


1 Answers

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

like image 146
Cygnusx1 Avatar answered Sep 20 '22 01:09

Cygnusx1