使用2个不同的数据源:Spring批处理 [英] Using 2 different datasources : Spring batch

查看:108
本文介绍了使用2个不同的数据源:Spring批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的数据源,一个用于阅读,另一个用于编写如下结果:

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


  • ItemReader应该从dataSource_1获取数据。

  • ItemWriter应该将数据写入dataSource_2。

知道读者和作者在同一个tasklet中。

knowing that reader and writer are in the same tasklet.

根据文档,我们可以在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?

我不能依赖容器而我不使用ORM层(JPA ..) ,我使用直接JDBC驱动程序读入数据库1并写入database2。

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.

当前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>

如何使用Spring Batch配置JTA / XA事务(Atomikos)?

How could i configure the JTA/XA transaction ( Atomikos ) with Spring Batch?

推荐答案

忘掉Spring Batch。

Forget about Spring Batch.

假设您需要编写一个应用程序服务,从一个datasource1读取,并写入datasource2,你需要做什么?

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

分布式事务是你的答案。这当然涉及额外的配置。如果您在J2EE容器(Websphere,Weblogic,JBoss等)中,那么它们提供的事务管理器应该能够处理分布式事务。您应该能够在Spring中为每个平台找到相应的TransactionManager实现。您需要的只是配置数据源(也应该在容器下)以使用XA感知驱动程序,并在您的应用程序中,使用相应的事务管理器,并通过JNDI查找XA感知数据源

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

但是如果你不能依赖容器(例如你正在编写一个独立的应用程序等),你需要找到一个能够做到这一点的事务管理器。 Atomikos 是最着名的免费JTA / XA库之一。

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.

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

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.

这篇关于使用2个不同的数据源:Spring批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆