首页 文章

Reg Spring批量交易

提问于
浏览
-2

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

1 回答

  • 0

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

    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; 
        }
    
    }
    

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

相关问题