具有多线程的Spring批处理错误

时间:2016-07-09 18:48:21

标签: multithreading spring-batch

我创建了一个存储过程读取器,它返回一个引用游标,我希望将来自引用游标的数据存储在oracle表中。我想在多线程中转换我的批处理应用程序,其中每个线程在从ref cursor接收的不同记录上工作存储proc.Currently我的批处理工作没有多线程,它正在将数据正确地插入表中。当我尝试转换多线程时,我面临奇怪的情况,其中一些记录由线程拾取而一些不是。< / p>

这是我的工作定义和taskExecutor。有时它也会给我错误说“意外的光标位置改变”。任何帮助都会非常感激。

<!-- TestUser Job definition -->
<job id="TestUserJob" xmlns="http://www.springframework.org/schema/batch"
    restartable=" true " incrementer="jobParametersIncrementer">
    <step id="step1" allow-start-if-complete="true">
        <tasklet task-executor="taskExecutor"  throttle-limit="5">
            <chunk reader="testUserItemReader" writer="testUserItemWriter" 
                commit-interval="1" />
        </tasklet>
    </step>
    <!-- Add step2 and so on HERE -->
</job>
<!-- Task executer definition -->
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="3" />
    <property name="maxPoolSize" value="4" /> <!--  put in variable like ${variable-name} -->
</bean> 
<bean id="testUserItemReader"
    class="org.springframework.batch.item.database.StoredProcedureItemReader">
    <property name="dataSource" ref="oracle_dataSource" />
    <property name="procedureName" value="get_user_func_no_arg" />
    <property name="parameters">
        <list>
            <bean class="org.springframework.jdbc.core.SqlParameter">
                <constructor-arg index="0" value="p_id_min" />
                <constructor-arg index="1">
                    <util:constant static-field="java.sql.Types.INTEGER" />
                </constructor-arg>
            </bean>
            <bean class="org.springframework.jdbc.core.SqlOutParameter">
                <constructor-arg index="0" value="p_recordset" />
                <constructor-arg index="1">
                    <util:constant static-field="oracle.jdbc.OracleTypes.CURSOR" />
                </constructor-arg>
            </bean>
        </list>
    </property>
    <property name="refCursorPosition" value="2" />
    <property name="rowMapper">
        <bean class="com.model.testUser.TestUserRowMapper" />
    </property>
    <property name="PreparedStatementSetter" ref="psTestUserSetter" />
</bean>

2 个答案:

答案 0 :(得分:0)

The StoredProcedureItemReader is not threadsafe due to the fact that we reference a single ResultSet in it. The only way to handle this in a thread safe manor will be to write your own ItemReader implementation or wrap ours in a way that handles the synchronization for you.

答案 1 :(得分:0)