Reg Spring批量交易

时间:2017-12-27 10:50:43

标签: spring spring-batch spring-transactions

我的要求是我需要将两个数据源连接到Spring Batch Application 1)一个用于Spring批处理作业和执行存储
2)用于业务数据识别,处理和撤销 我知道有很多解决方案可以实现这一目标。但我通过将第二个数据源设置为主数据来实现。问题是第二个数据源不在事务范围内,而是为每个执行sql语句提交,特别是通过jdbctemplate执行。

1 个答案:

答案 0 :(得分:0)

因为我无法编辑我的问题。我正在详细撰写另一篇文章 我的要求是我需要将两个数据源连接到Spring Batch Application。
1)一个用于Spring批处理作业和执行存储
2)一个用于业务数据Stroing,Processing和Retreiving。

env-context.xml 中,我有以下配置

rand_num

override-context.xml 中,我有以下代码

<!-- Enable annotations-->
<context:annotation-config/>

<bean primary="true" id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/DB2XADS"/>
</bean>

<!-- Creating TransactionManager Bean, since JDBC we are creating of type 
    DataSourceTransactionManager -->
<bean id="transactionManager" primary="true"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- jdbcTemplate uses dataSource -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" /> 
</bean>

<bean id="batchTransactionManager" 
    class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

<bean id="transactionTemplate"
    class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager" />
</bean>

job-config.xml 中,我有以下代码

<tx:annotation-driven transaction-manager="transactionManager" />

<!-- jdbcTemplate uses dataSource -->
<bean id="batchDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:/MySqlDS"/>
</bean>

<bean class="com.honda.pddabulk.utility.MyBatchConfigurer">
    <property name="dataSource" ref="batchDataSource" />
</bean>

<!-- Use this to set additional properties on beans at run time -->
<bean id="placeholderProperties"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties
            </value>
           <value>classpath:/batch/batch-mysql.properties</value>
            <value>classpath:log4j.properties</value>
        </list>
    </property>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="order" value="1"/>
</bean>

<!-- Overrider job repository -->
<bean id="jobRepository"
      class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="databaseType" value="mysql"/>
    <property name="dataSource" ref="batchDataSource"/>
    <property name="tablePrefix" value="${batch.table.prefix}"/>
    <property name="maxVarCharLength" value="2000"/>
    <property name="isolationLevelForCreate" value="ISOLATION_SERIALIZABLE"/>
    <property name="transactionManager" ref="batchTransactionManager"/>
</bean>

<!-- Override job service -->
<bean id="jobService" class="org.springframework.batch.admin.service.SimpleJobServiceFactoryBean">
    <property name="tablePrefix" value="${batch.table.prefix}"/>
    <property name="jobRepository" ref="jobRepository"/>
    <property name="jobLauncher" ref="jobLauncher"/>
    <property name="jobLocator" ref="jobRegistry"/>
    <property name="dataSource" ref="batchDataSource"/>
</bean>

<!-- Override job launcher -->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor" ref="jobLauncherTaskExecutor" />
</bean>

<task:executor id="jobLauncherTaskExecutor" pool-size="21" rejection-policy="ABORT" />

<!-- Override job explorer -->
<bean id="jobExplorer"
      class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
    <property name="tablePrefix" value="${batch.table.prefix}"/>
    <property name="dataSource" ref="batchDataSource"/>
</bean>

我有自定义Batch configurer set。现在问题是当我尝试使用jdbctemplate执行查询以进行更新并插入时,它不在事务中,这意味着@Transactional不起作用。

而是为每个方法调用进行提交。例子是

<context:component-scan base-package="com.honda.*">
     <context:exclude-filter type="regex" 
               expression="com.honda.pddabulk.utility.MyBatch*" /> 
</context:component-scan>

在上面的代码中我试图插入数据并立即我也抛出一个异常,这意味着插入应该发生,但提交不会。所以我们将无法看到任何数据,但遗憾的是提交正在进行。请一些帮助