聞かれたのでメモ。
Spring BatchはデフォルトでJob管理のためのメタデータテーブルをDBに作成する。
Spring Bootを使うと設定不要でそのテーブルが作成され、アクセスするためのJobRepository
などが作られる。
ただ、これがDataSource
が一個しか登録されていないことを前提にしているので、本ジョブで複数のDataSource
を使おうとするとエラーになる。
java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2
この時はJobRepository
用のDataSource
に@Primary
をつけて、次のようなBatchConfigurer
を用意しておけば良い。
@Bean
@ConfigurationProperties(prefix = "spring.datasource1")
@Primary
DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
DefaultBatchConfigurer batchConfigurer(DataSource dataSource) {
return new DefaultBatchConfigurer(dataSource);
}
メタデータテーブルをそもそも作りたくないという場合は次の記事を参照。