Spring批处理:如何更改默认隔离级别?

Spring Batch : How to change the default isolation level?

我阅读了文档:

"The default is ISOLATION_SERIALIZABLE, which prevents accidental concurrent execution of the SAME job"

但是,当我同时启动不同的作业时(默认隔离级别为 SERIALIZABLE),出现错误:ORA-08177: can't serialize access for this transaction. 正常吗?

其次,要将默认隔离级别更改为 READ_COMMITTED,我知道我们无法在 application.properties 中更改此级别,并且我必须重新定义 BatchConfigurer。准确吗?

使用 BasicBatchConfigurer,我必须定义一个显式构造函数(默认构造函数的隐式超级构造函数 BasicBatchConfigurer() 未定义)。 但是,我有错误:

 Parameter 0 of constructor in MyBatchConfigurer required a bean of type 'org.springframework.boot.autoconfigure.batch.BatchProperties' that could not be found.

如何创建:BatchProperties 属性、DataSource 数据源和 TransactionManagerCustomizers transactionManagerCustomizers?

这是我的代码:

PeopleApplication.java

@SpringBootApplication
@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })
public class PeopleApplication {
    
    
    public static void main(String[] args) throws Exception {
        
        ConfigurableApplicationContext ctx =  new SpringApplicationBuilder(PeopleApplication.class)
        .web(WebApplicationType.NONE) 
        .run(args);
        
        int exitValue = SpringApplication.exit(ctx);
        System.exit(exitValue);
    }
}

MyBatchConfigurer.java

@Component
@PropertySource("classpath:fileA.properties")
public class MyBatchConfigurer extends BasicBatchConfigurer implements CommandLineRunner, ExitCodeGenerator {

    protected MyBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) {
         super(properties, dataSource, transactionManagerCustomizers);
     }
     
     @Override
     protected String determineIsolationLevel() {
            return "ISOLATION_" + Isolation.READ_COMMITTED;
     }

    @Override
    public void run(String... args) {
        ...
    }

    ...
}

此致。


响应:


使用 @EnableAutoConfiguration 而不是 :

@EnableAutoConfiguration(exclude = { BatchAutoConfiguration.class })

像这样,将自动创建 Bean BatchProperties、DataSource 和 TransactionManagerCustomizers。

请看Mahmoud的回复,解释的很清楚

.

用法示例如下,仅覆盖隔离级别

--application.properties

spring.application.name=SpringBatch

####### SPRING ##############

spring.main.banner-mode=off
spring.main.web-application-type=none
spring.batch.initialize-schema=always
spring.batch.job.enabled=false // Disable default if you want to control

########JDBC Datasource########
#connection timeout 10 min
spring.datasource.hikari.connection-timeout=600000
spring.datasource.hikari.minimum-idle=5 
spring.datasource.hikari.maximum-pool-size=100
spring.datasource.hikari.idle-timeout=600000 
spring.datasource.hikari.max-lifetime=1800000 
spring.datasource.hikari.auto-commit=true 
spring.datasource.hikari.poolName=SpringBoot-HikariCP
spring.datasource.url=jdbc:oracle:thin:@ngecom.ae:1521:ngbilling
spring.datasource.username=ngbilling
springbatch.datasource.password=ngbilling

@SpringBootApplication
public class YourApplication {

    public static void main(String[] args) {
        SpringApplication.run(SsadapterApplication.class, args);
    }

}

// Your manual batch scheduler

 class BatchJobScheduler extends BasicBatchConfigurer {
        @Autowired
        private JobLauncher jobLauncher;
        @Autowired
        private ApplicationArguments args;
        @Autowired
        private Job myJob;
        protected BatchJobScheduler(BatchProperties properties, DataSource dataSource,
                TransactionManagerCustomizers transactionManagerCustomizers) {
            super(properties, dataSource, transactionManagerCustomizers);
        }
        @Override
        protected String determineIsolationLevel() {
            return "ISOLATION_" + Isolation.READ_COMMITTED;
        }
        //@Scheduled(cron = "${batch.cron}")
        public void notScheduledJob() {
                appId= args.getOptionValues("appId").get(0);
                JobParameters params = new JobParametersBuilder().addLong("jobId"+appId, System.currentTimeMillis())
                        .toJobParameters();
                jobLauncher.run(myJob, params);
        }
       

// Your batch Configuration And Spring will do everything for you to available

@Configuration
@EnableBatchProcessing
@EnableScheduling
public class BatchConfiguration {

    Logger logger = LoggerFactory.getLogger(BatchConfiguration.class);
    
  
    @Value("${batch.commit.chunk}")
    private Integer chunkSize;

    @Value("${batch.error.skipCount}")
    private Integer skipErrorCount;
    
    
    @Autowired
    private Environment environment;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private ApplicationArguments args;
    
    @Autowired
    private JdbcTemplate jdbcTemplate; 

    @Bean
    public Job myJob() throws Exception {
        return jobBuilderFactory.get("myJob").incrementer(new RunIdIncrementer())
                .listener(new JobListener()).start(myStep()).build();
    } 

   @Bean
    public Step myStep() throws Exception {
        return stepBuilderFactory.get("myStep").<InputObject, OutPutObject>chunk(chunkSize)
                .reader(yourReader(null)).processor(yourProcessor()).writer(yourWriter())
                //.faultTolerant()
                //.skipLimit(skipErrorCount).skip(Exception.class)//.noSkip(FileNotFoundException.class)
                .listener(invoiceSkipListener())
                //.noRetry(Exception.class)
                //.noRollback(Exception.class)
                .build();
    }