春季批次隔离级别

问题描述

我有一个Spring Batch作业,它每天使用不同的参数依次启动(下一个在上一个完成时启动)。 有时,我从数据库中收到ORA-08177错误

为防止这种情况,我设置了一个customJobRepository:

@Bean
    public Job createCamt(...
                          JobRepository customJobRepository) {
        return jobBuilderFactory.get("myJob")
                                .repository(customJobRepository)
                                ...
                                .build();
    }

    @Bean
    public JobRepository customJobRepository(
            @Qualifier("centralDataSource") DataSource centralDataSource,@Qualifier("centralTransactionManager") PlatformTransactionManager centralTransactionManager
    ) throws Exception {
        JobRepositoryfactorybean factorybean = new JobRepositoryfactorybean();
        factorybean.setDatabaseType("ORACLE");
        factorybean.setDataSource(centralDataSource);
        factorybean.setTransactionManager(centralTransactionManager);
        factorybean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
        return factorybean.getobject();
    }

它似乎不起作用,因为我仍然从数据库中收到ORA-08177错误。 我在sql中查看了是否可以使用隔离级别read commited打开事务,并且可以正常工作:

SPRING_BATCH> set transaction isolation level read committed
[2020-10-05 10:35:43] completed in 3 ms
  1. 为什么Spring Batch的隔离级别不在read committed上?
  2. 当每个作业单独运行时,怎么会发生此错误

解决方法

您的自定义JobRepository未考虑在内,仅将其声明为bean是不够的。如果要使用自定义JobRepository,则需要提供BatchConfigurer

这在参考文档的Configuring a JobRepository部分中进行了说明。