您能告诉我应该在Spring Batch Application in Production中使用哪个事务管理器吗?我正在使用Resourceless Transaction manager。好吗?从外部Oracle DB中读取数据时我遇到了这个问题
[org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler] (pool-3130-thread-1) Unexpected error occurred in scheduled task.: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLRecoverableException: Closed Connection
@Bean
public ResourcelessTransactionManager resourcelessTransactionManager() {
return new ResourcelessTransactionManager();
}
@Bean
public MapJobRepositoryFactoryBean mapJobRepositoryFactory(
ResourcelessTransactionManager txManager) throws Exception {
//LOGGER.info("Inside mapJobRepositoryFactory method");
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(txManager);
factory.setTransactionManager(txManager);
factory.setIsolationLevelForCreate("ISOLATION_READ_UNCOMMITTED");
factory.afterPropertiesSet();
return factory;
}
@Bean
public JobRepository jobRepository(
MapJobRepositoryFactoryBean factory) throws Exception {
//LOGGER.info("Inside jobRepository method");
return factory.getObject();
}
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(30);
return taskExecutor;
}
@Bean
public JobLauncher jobLauncher(JobRepository jobRepository,ThreadPoolTaskExecutor taskExecutor) {
//LOGGER.info("Inside jobLauncher method");
SimpleJobLauncher launcher = new SimpleJobLauncher();
launcher.setTaskExecutor(taskExecutor);
launcher.setJobRepository(jobRepository);
final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
launcher.setTaskExecutor(simpleAsyncTaskExecutor);
return launcher;
}
答案 0 :(得分:0)
正如您在问题中所说,您使用的是Oracle DB,因此您很可能不需要ResourcelessTransactionManager
。
您当前的代码正在做的是将作业元数据存储到基于地图的 in memory 结构中,我的猜测是你不会在生产中这样做而你实际上是在存储作业DB中的元数据 - 用于以后的分析,重启能力等
在Spring Batch中,有两种交易 - 一种用于业务数据和方法,另一种用于作业存储库,并且假设您想要从文件中读取,写入文件并希望将作业元数据存储到{ {1}}那么你的代码就行了。
但是,当您定义MapJobRepository
时,您无法使用DataSource
。事实上,对于数据库,您不需要自己定义任何事务管理器,但Spring Batch面向块的处理将自行处理并将作业元数据存储到数据库中。