InvalidDataAccessResourceUsageException:意外的光标位置更改

时间:2013-12-04 22:55:43

标签: spring exception jdbc spring-batch

尝试从Spring Batch应用程序中的数据库读取时遇到以下异常。任何见解都会有所帮助。这是在使用JdbcCursorItemReader时引起的。使用JdbcPagingItemReader可以正常运行该应用程序。

使用JdbcCursorItemReader时还需要显式关闭任何资源(游标等)吗?如果是这样,我们该怎么做?

SEVERE: Encountered an error executing the step
    org.springframework.dao.InvalidDataAccessResourceUsageException: Unexpected cursor position change.
        at org.springframework.batch.item.database.AbstractCursorItemReader.verifyCursorPosition(AbstractCursorItemReader.java:365)
        at org.springframework.batch.item.database.AbstractCursorItemReader.doRead(AbstractCursorItemReader.java:449)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:695)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:132)
        at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:120)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
        at com.testapp.springbatchtest.io.DataExtractCursorItemReader$$EnhancerByCGLIB$$748012e7.doRead(<generated>)
        at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:83)
        at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
        at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:155)
        at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:114)
        at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
        at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
        at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
        at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:108)
        at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
        at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:395)
        at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
        at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:267)
        at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
        at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
        at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
        at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
        at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:253)
        at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:195)
        at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
        at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:380)
        at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:124)
        at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
        at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
        at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
        at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)

以下是bean声明的示例代码。 DataExtractCursorItemReader扩展了JdbcCursorItemReader。

@Bean
@StepScope
public DataExtractCursorItemReader cursorReaderBean() throws Exception {
    DataExtractCursorItemReader dataExtractReader = new DataExtractCursorItemReader();
    dataExtractReader.setDataSource(dataSource);
    dataExtractReader.setSql("SELECT * FROM SAMPLETABLE");
    dataExtractReader.setFetchSize(500);
    dataExtractReader.setRowMapper(new DataExtractRowMapper());

    return dataExtractReader;
}

3 个答案:

答案 0 :(得分:2)

verifyCursorPosition属性(似乎)只是检查以防止用户在继承读取器时进行索引操作;也许是你的情况,或者某些数据库平台属性可能不起作用。

你可以禁用它,一切都应该正常工作 - 这只是一个防止子类化的测试 如果您修改光标位置或检查测试是否因数据库驱动程序而失败,请检查您的代码。

答案 1 :(得分:1)

如果有多个线程作用于数据库读取器且读取器不是线程安全的,则会看到此错误 通常是JDBCCursorItemReader与JDBCPagingItemReader

答案 2 :(得分:1)

verifyCursorPosition 检查取决于java.sql.ResultSet :: getRow()。 &#34;对结果集类型为TYPE_FORWARD_ONLY&#34;的ResultSets,getRow方法的支持是可选的。根据{{​​3}}

您可以尝试更改驱动程序,ResultSet类型(Reader),或按照上述建议禁用 verifyCursorPosition