Spring Batch:如何更改默认隔离级别



我阅读了文档:

"默认值为ISOLATION_SERIALIZABLE,可防止意外同时执行同一作业";

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

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

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

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 transactionManagerCustomizer?

这是我的代码:

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的回复,该回复非常清楚地解释了

可以';t在spring-batch中序列化对此事务的访问。

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

--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();
}

最新更新