Spring 启动 + Spring 批处理 + HSQLDB -> 为 JobRepository 配置 HSQLDB
Spring Boot + Spring Batch + HSQLDB -> Configure HSQLDB for JobRepository
我是 Spring Boot 新手。我正在尝试编写一个应用程序,利用 Spring Batch 将数据从一个数据库写入另一个数据库。我正在从 Mongo 读取并写入 SQL 服务器。我在内存中托管我的 JobRepository 并将其编码为使用 HSQLDB.
我在将 Spring.Batch 指向正确的数据源时遇到了一些问题,所以我现在专注于确保我正在将我的作业写入 HSQLDB。为此,我现在基本上已经注释掉了 SQL 数据库。
我有以下 BatchConfiguration class:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Autowired
@Qualifier("batchDataSource")
public DataSource batchDataSource;
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
public JdbcTransactionManager batchTransactionManager() {
final JdbcTransactionManager transactionManager = new JdbcTransactionManager();
transactionManager.setDataSource(batchDataSource);
return transactionManager;
}
@Bean
public JobRepository jobRepositoryBean() throws Exception {
JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
fb.setDatabaseType("HSQL");
fb.setDataSource(batchDataSource);
fb.setTransactionManager(batchTransactionManager());
return fb.getObject();
}
@Override
@Bean
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(this.jobRepositoryBean());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
当我加载这个 class 时,我在日志中看到以下输出:
2021-06-24 17:18:42.412 WARN 31940 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a DataSourceTransactionManager
2021-06-24 17:18:42.419 INFO 31940 --- [ main] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: HSQL
2021-06-24 17:18:43.154 WARN 31940 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
我想了解为什么要推断数据库类型。看来我已经设置好了。此外,当我取消注释其他数据源时,推断的类型是 SQLSERVER,这是错误的。我是否错误地初始化和设置了这种数据类型的配置?
我想在再次处理多数据源情况之前,我会尝试让它运行良好。
仅将 TransactionManager
和 JobRepository
声明为 bean 是不够的,Spring 批处理不会考虑这些内容。您需要为此提供 BatchConfigurer
。由于您使配置 class 扩展 DefaultBatchConfigurer
,您可以覆盖 getTransactionManager
和 getJobRepository
,例如:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Autowired
@Qualifier("batchDataSource") // ensure this is the HSQLDB here
public DataSource batchDataSource;
@Override
public JdbcTransactionManager getTransactionManager() {
final JdbcTransactionManager transactionManager = new JdbcTransactionManager();
transactionManager.setDataSource(batchDataSource);
return transactionManager;
}
@Override
public JobRepository getJobRepository() throws Exception {
JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
fb.setDatabaseType("HSQL");
fb.setDataSource(batchDataSource);
fb.setTransactionManager(getTransactionManager());
return fb.getObject();
}
@Override
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(this.jobRepositoryBean());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
这在此处的参考文档中有解释:Java Config。
我是 Spring Boot 新手。我正在尝试编写一个应用程序,利用 Spring Batch 将数据从一个数据库写入另一个数据库。我正在从 Mongo 读取并写入 SQL 服务器。我在内存中托管我的 JobRepository 并将其编码为使用 HSQLDB.
我在将 Spring.Batch 指向正确的数据源时遇到了一些问题,所以我现在专注于确保我正在将我的作业写入 HSQLDB。为此,我现在基本上已经注释掉了 SQL 数据库。
我有以下 BatchConfiguration class:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Autowired
@Qualifier("batchDataSource")
public DataSource batchDataSource;
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
@Bean
public JdbcTransactionManager batchTransactionManager() {
final JdbcTransactionManager transactionManager = new JdbcTransactionManager();
transactionManager.setDataSource(batchDataSource);
return transactionManager;
}
@Bean
public JobRepository jobRepositoryBean() throws Exception {
JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
fb.setDatabaseType("HSQL");
fb.setDataSource(batchDataSource);
fb.setTransactionManager(batchTransactionManager());
return fb.getObject();
}
@Override
@Bean
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(this.jobRepositoryBean());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
当我加载这个 class 时,我在日志中看到以下输出:
2021-06-24 17:18:42.412 WARN 31940 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a DataSourceTransactionManager
2021-06-24 17:18:42.419 INFO 31940 --- [ main] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: HSQL
2021-06-24 17:18:43.154 WARN 31940 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
我想了解为什么要推断数据库类型。看来我已经设置好了。此外,当我取消注释其他数据源时,推断的类型是 SQLSERVER,这是错误的。我是否错误地初始化和设置了这种数据类型的配置?
我想在再次处理多数据源情况之前,我会尝试让它运行良好。
仅将 TransactionManager
和 JobRepository
声明为 bean 是不够的,Spring 批处理不会考虑这些内容。您需要为此提供 BatchConfigurer
。由于您使配置 class 扩展 DefaultBatchConfigurer
,您可以覆盖 getTransactionManager
和 getJobRepository
,例如:
@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {
@Autowired
@Qualifier("batchDataSource") // ensure this is the HSQLDB here
public DataSource batchDataSource;
@Override
public JdbcTransactionManager getTransactionManager() {
final JdbcTransactionManager transactionManager = new JdbcTransactionManager();
transactionManager.setDataSource(batchDataSource);
return transactionManager;
}
@Override
public JobRepository getJobRepository() throws Exception {
JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
fb.setDatabaseType("HSQL");
fb.setDataSource(batchDataSource);
fb.setTransactionManager(getTransactionManager());
return fb.getObject();
}
@Override
public JobLauncher getJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(this.jobRepositoryBean());
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
这在此处的参考文档中有解释:Java Config。