要使用默认的BatchConfigurer,上下文中最多只能包含一个数据源,找到2 [英] To use the default BatchConfigurer the context must contain no more thanone DataSource, found 2

查看:430
本文介绍了要使用默认的BatchConfigurer,上下文中最多只能包含一个数据源,找到2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Boot和Spring Batch.对于meta表,我想使用mysql;对于所有业务,我想使用db2作为数据库.当我实现获取错误时.

I am using spring boot and spring batch. For meta table i want to use mysql and for all business thing i want to use db2 as a database.When i implemented getting error.

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

我找到了一个吉拉 https://jira.spring.io/browse/BATCH-2537 ,其中说带有@EnableBatchProcessing的@Primary数据源不适用于使用多个数据源",如果正确,请告诉我如何实现相同的目的. 我还发现了如何在spring中使用2个或更多数据库? ,但对我的情况没有帮助.

I found one jira https://jira.spring.io/browse/BATCH-2537 which say that "@Primary datasource with @EnableBatchProcessing does not work for using multiple datasources" if this is correct please tell me how i can achive same. I also found this How to use 2 or more databases with spring? but not helpful in my case.

推荐答案

正如Deinum先生所建议的那样,我还将数据源的配置放在单独的文件中.

As M. Deinum suggested, I would also put the configuration of the datasources in a separate file.

除此之外,您的配置文件中还存在以下问题:

Beside that, there are the following problems in your configuration file:

  1. SpringBatch寻找一个名为"dataSource"的数据源(注意大写的S).如果找不到,它将查找找到的任何数据源.但是,如果发现多个,则会引发异常->您观察到的异常.

  1. SpringBatch looks for a datasource named "dataSource" (note the capital S). If it doesn't find one, it looks for any datasource it finds. However, if it finds more than one, it throws an exception -> the one you observed.

在配置文件中,创建两个数据源并注入一个(@Autowired数据源dataSourceSecond).这将导致下一个问题,因为您没有使用此名称的数据源. (您仅定义了数据源"secondaryDataSource"和"primaryDataSource").这也会导致异常.

In your configuration file, you create two datasources and inject one (@Autowired Datasource dataSourceSecond). This would cause the next problem, since you don't have a datasource with this name. (You only defined the datasources "secondaryDataSource" and "primaryDataSource"). This would also lead to an exception.

这是我如何组织我的配置

Here is how I would organise my configurations

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

   ...

我也写了一个更彻底的回答类似的问题:

I've also written an more thouroughly answer to a similar question: I would like to create a spring batch project where batch does not use my datasource

这篇关于要使用默认的BatchConfigurer,上下文中最多只能包含一个数据源,找到2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆