【问题标题】:Reg Spring Batch TransactionReg Spring 批量事务
【发布时间】:2017-12-27 10:50:42
【问题描述】:

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

【问题讨论】:

  • 你能通过分享一些代码来解释......更多关于你的问题
  • 嗨 Yogi 我无法编辑我提出的问题,因此我将我的问题详细发布为同一问题的答案。所以请看一下并指导我提前谢谢

标签: spring spring-batch spring-transactions


【解决方案1】:

因为我无法编辑我的问题。我正在详细写另一篇文章
我的要求是我需要有两个数据源连接到 Spring Batch 应用程序。
1) 一个用于存储 Spring Batch Jobs 和 Executions
2) 一种用于业务数据检索、处理和检索。

env-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>

override-context.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>

job-config.xml 我有以下代码

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

我设置了自定义批处理配置器。现在的问题是,当我尝试使用 jdbctemplate 执行查询以进行更新并插入它时,它不在事务下,这意味着 @Transactional 不起作用。

每个方法调用都会发生提交。例子是

@Transactional
public void checkInsertion() throws Exception{
    try{
        jdbcTemplate.update("INSERT INTO TABLE_NAME(COLUMN1, COLUMN2) VALUES( 'A','AF' );
        throw new PddaException("custom error");
    }catch(Exception ex){
        int count=jdbcTemplate.update("ROLLBACK");
        log.info("DATA HAS BEEN ROLLBACKED SUCCESSFULLY... "+count);        
        throw ex; 
    }

}

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

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多