spring InvalidDataAccessResourceUsageException:意外的光标位置更改

x4shl7ld  于 2023-03-28  发布在  Spring
关注(0)|答案(4)|浏览(181)

尝试从Spring Batch Application中的Database读取时遇到以下异常。任何见解都将有所帮助。这是在使用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;
}
kpbwa7wx

kpbwa7wx1#

verifyCursorPosition属性(似乎)只是一个检查,以防止用户在子类化reader时操纵索引;也许是你的情况,也许在某些DB平台上属性不起作用。
你可以禁用它,一切都应该正常工作-这只是一个测试,以防止子类化。
如果修改了光标位置,请检查代码,或者检查测试是否因数据库驱动程序而失败。

stszievb

stszievb2#

我在有多个线程作用于数据库读取器并且读取器不是线程安全的情况下看到此错误通常是JDBCCursorItemReader vs JDBCPagingItemReader

5hcedyr0

5hcedyr03#

verifyCursorPosition检查依赖于java.sql.ResultSet::getRow()。根据JavaDoc“对于结果集类型为TYPE_FORWARD_ONLY的ResultSets,getRow方法的支持是可选的”

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

brvekthn

brvekthn4#

如果我们从Job中删除taskExecutors,这将起作用。它最终将删除多线程。

相关问题