Mar 1, 2017
Mar 1, 2017
N/A Views
MD
warning
この記事は2年以上前に更新されたものです。情報が古くなっている可能性があります。

聞かれたのでメモ。

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

メタデータテーブルをそもそも作りたくないという場合は次の記事を参照。

Found a mistake? Update the entry.
Share this article: