简体   繁体   English

使用2个不同的数据源:Spring批处理

[英]Using 2 different datasources : Spring batch

I have 2 different datasources , one to read and another one to write results like below: 我有2个不同的数据源 ,一个用于读取,另一个用于写入如下结果:

  • ItemReader should get data from dataSource_1. ItemReader应该从dataSource_1获取数据。
  • ItemWriter should write data to dataSource_2. ItemWriter应该将数据写入dataSource_2。

knowing that reader and writer are in the same tasklet. 知道读者和作者在同一个tasklet中。

As per the documentation, we can configure a single transaction manager at tasklet 根据文档,我们可以在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. 我不能依赖容器,我不使用ORM层(JPA ..),我使用直接JDBC驱动程序读入数据库1并写入database2。

current conf : 当前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? 我如何使用Spring Batch配置JTA / XA事务(Atomikos)?

You will need to use a XA compatible driver for your 2 data-sources with a JTA transaction Manager. 您需要使用与JTA事务管理器的2个数据源的XA兼容驱动程序。

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. 如果读者可以在事务之外,则只能使用writer的trx管理器。 If you need the reader and the writer in the same transaction, probably you need a XA compatible transaction manager. 如果您需要同一事务中的阅读器和编写器,可能需要一个兼容XA的事务管理器。

Forget about Spring Batch. 忘记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? 假设您需要编写一个从一个datasource1读取的应用程序服务,并写入datasource2,您需要做什么?

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. 如果您在J2EE容器(Websphere,Weblogic,JBoss等)中,那么它们提供的事务管理器应该能够处理分布式事务。 You should be able to find the corresponding TransactionManager implementation in Spring for each of these platforms. 您应该能够在Spring中为每个平台找到相应的TransactionManager实现。 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 您需要的只是配置数据源(也应该在容器下)以使用XA感知驱动程序,并在您的应用程序中,使用相应的事务管理器,并通过JNDI查找XA感知数据源

However if you cannot rely on the container (eg 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. Atomikos是最着名的免费JTA / XA库之一。

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. 当然,如果您百分之百确定所有事务感知操作都在datasource2中,您可以考虑仅将数据源事务管理器用于datasource2,但老实说,这不是我建议的首选方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM