Чтобы использовать BatchConfigurer по умолчанию, контекст должен содержать не более одного источника данных, найден 2
Я использую весеннюю загрузку и весеннюю партию. Для мета таблицы я хочу использовать mysql, а для всего бизнеса я хочу использовать db2 в качестве базы данных. Когда я реализовал получение ошибки.
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/local
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.seconddatasource.url=jdbc:db2://***************
spring.seconddatasource.username=******
spring.seconddatasource.password=****
spring.seconddatasource.driverClassName=com.ibm.db2.jcc.DB2Driver
BatchCongig.java
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSourceSecond;
@Bean
@ConfigurationProperties(prefix="spring.seconddatasource")
public javax.sql.DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public javax.sql.DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public JdbcCursorItemReader<User> reader()
{
JdbcCursorItemReader<User> reader=new JdbcCursorItemReader<>();
reader.setDataSource(dataSourceSecond);
reader.setSql("Select ACCT_ID from ACCT_table FETCH FIRST 100 ROWS ONLY");
reader.setRowMapper(new UserRowerMapper());
return reader;
}
@Bean
public UserItemProcessor processor()
{
return new UserItemProcessor();
}
@Bean
public Step step1()
{
return stepBuilderFactory.get("step1").<User,User>chunk(10)
.reader(reader())
.processor(processor())
.build();
}
@Bean
public Job job1()
{
return jobBuilderFactory.get("jobakjkkj")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
}
стек ошибок
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.schwab.cat.SpringBatchDbReadApplication.main(SpringBatchDbReadApplication.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2
at org.springframework.batch.core.configuration.annotation.AbstractBatchConfiguration.getConfigurer(AbstractBatchConfiguration.java:108) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration.initialize(SimpleBatchConfiguration.java:114) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$ReferenceTargetSource.createObject(SimpleBatchConfiguration.java:142) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.aop.target.AbstractLazyCreationTargetSource.getTarget(AbstractLazyCreationTargetSource.java:86) ~[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192) ~[spring-aop-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at com.sun.proxy.$Proxy46.getJobInstances(Unknown Source) ~[na:na]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.getNextJobParameters(JobLauncherCommandLineRunner.java:131) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:212) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:231) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:123) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:117) ~[spring-boot-autoconfigure-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
... 6 common frames omitted
Я нашел один jira https://jira.spring.io/browse/BATCH-2537 который говорит, что "@Primary источник данных с @EnableBatchProcessing не работает для использования нескольких источников данных", если это правильно, пожалуйста, скажите мне, как я могу достичь того же самого. Я также нашел это Как использовать 2 или более баз данных с пружиной? но не полезно в моем случае.
1 ответ
Как предложил М. Дейнм, я бы также поместил конфигурацию источников данных в отдельный файл.
Кроме того, в вашем файле конфигурации есть следующие проблемы:
SpringBatch ищет источник данных с именем "dataSource" (обратите внимание на заглавную S). Если он не находит, он ищет любой источник данных, который он находит. Однако, если он находит более одного, он выдает исключение -> тот, который вы наблюдали.
В вашем файле конфигурации вы создаете два источника данных и внедряете один (@Autowired Datasource dataSourceSecond). Это может вызвать следующую проблему, так как у вас нет источника данных с этим именем. (Вы определили только источники данных "primaryDataSource" и "primaryDataSource"). Это также приведет к исключению.
Вот как я бы организовал свои конфигурации
@Configuration
public DatasourceConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.seconddatasource")
public javax.sql.DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
// note the new name: dataSource -> this is the name springBatch is looking for
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public javax.sql.DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}
@Configuration
@EnableBatchProcessing
@Import(DatasourceConfiguration.class)
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
// note the name
@Autowired
public DataSource secondaryDataSource;
@Bean
public JdbcCursorItemReader<User> reader()
{
JdbcCursorItemReader<User> reader=new JdbcCursorItemReader<>();
// note the name
reader.setDataSource(secondaryDataSource);
reader.setSql("Select ACCT_ID from ACCT_table FETCH FIRST 100 ROWS ONLY");
reader.setRowMapper(new UserRowerMapper());
return reader;
}
...
Я также написал более подробный ответ на аналогичный вопрос: я хотел бы создать проект с использованием пакетной пружины, в котором пакет не использует мой источник данных.