如何修改Spring Batch事务隔离级别?附JobLauncher启动Job代码
Hey there! Let's break down how to adjust the transaction isolation level in Spring Batch based on your scenario. Here are the common approaches you can take:
Since you're using JobLauncher to start your job, you can customize its transaction behavior by setting a TransactionTemplate with your desired isolation level. This applies to the transaction that wraps the job startup process:
@RestController public class SystemController { static Logger logger = LoggerFactory.getLogger(SystemController.class); JobExecution jobExecution; @Autowired JobLauncher jobLauncher; @Autowired Job job; // ... your existing request mapping // Define a custom JobLauncher bean with specified transaction isolation @Bean public JobLauncher customJobLauncher(JobRepository jobRepository, PlatformTransactionManager transactionManager) { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository); TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager); // Replace with your target isolation level transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); jobLauncher.setTransactionTemplate(transactionTemplate); return jobLauncher; } @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
If you need granular control (e.g., a specific step requires a different isolation level than others), you can configure the transaction attributes directly on the step:
@Bean public Step myCustomStep(ItemReader<Data> reader, ItemProcessor<Data, Data> processor, ItemWriter<Data> writer, StepBuilderFactory stepBuilderFactory, PlatformTransactionManager transactionManager) { return stepBuilderFactory.get("myCustomStep") .<Data, Data>chunk(10) .reader(reader) .processor(processor) .writer(writer) .transactionAttribute(customTransactionAttribute()) .build(); } private DefaultTransactionAttribute customTransactionAttribute() { DefaultTransactionAttribute attribute = new DefaultTransactionAttribute(); // Use your desired isolation level here attribute.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); attribute.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); return attribute; }
Spring Batch uses the JobRepository to store metadata like job executions and step executions. You can set the isolation level for these internal operations:
@Bean public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); // Set isolation level for creating job execution records factory.setIsolationLevelForCreate("ISOLATION_SERIALIZABLE"); factory.setTablePrefix("BATCH_"); // Match your batch table prefix if applicable return factory.getObject(); }
Available Isolation Levels
You can use any of the standard Spring TransactionDefinition constants for isolation levels:
TransactionDefinition.ISOLATION_DEFAULT: Uses the database's default isolation levelTransactionDefinition.ISOLATION_READ_UNCOMMITTEDTransactionDefinition.ISOLATION_READ_COMMITTEDTransactionDefinition.ISOLATION_REPEATABLE_READTransactionDefinition.ISOLATION_SERIALIZABLE
内容的提问来源于stack exchange,提问作者Naveen Kumar Mishra

