当前位置: 首页 > 知识库问答 >
问题:

要使用默认的BatchConfigurer,上下文必须包含不超过一个数据源(找到2)

商泽宇
2023-03-14

我使用的是spring boot和spring batch。对于元表,我想使用mysql,对于所有业务,我想使用db2作为数据库。当我实现获取错误时。

应用属性

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

我发现了一个jirahttps://jira.spring.io/browse/BATCH-2537上面写着“@EnableBatchProcess的主数据源不适用于使用多个数据源”,如果这是正确的,请告诉我如何实现相同的结果。我还发现了这个如何使用2个或更多的Spring数据库?但对我的情况没有帮助。

共有1个答案

李洋
2023-03-14

正如M.Deinum所建议的,我还将把数据源的配置放在一个单独的文件中。

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

>

  • SpringBatch寻找一个名为“datasource”的数据源(注意大写字母S)。如果找不到,它会查找找到的任何数据源。但是,如果它发现多个异常,就会抛出一个异常-

    在您的配置文件中,您创建了两个数据源并注入了一个(@自动加载数据源数据源第二)。这将导致下一个问题,因为您没有具有此名称的数据源。(您只定义了数据源“第二数据源”和“主要数据源”)。这也会导致异常。

    以下是我将如何组织我的配置

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

    我还为一个类似的问题写了一个更深思熟虑的答案:我想创建一个spring batch项目,其中batch不使用我的数据源

  •  类似资料:
    • 我已经包含了okhttp3,并在比LOLLIPOP(api 21)更少的android版本中测试了补丁和删除方法。我没有包括Okio和它的工作很好。但是,在okhttp站点中,他们提到了以下内容: 您还需要Okio,OkHttp使用它来实现快速I/O和可调整大小的缓冲区。下载最新的JAR。 附加信息:我在Android 4.1.2 Samsung Galaxy Note中看到了有和没有okio 1

    • 问题内容: 包含的封闭实例 下面是代码。是我要使用的对象,它给了我上面的错误。 目前尚不清楚原因。 问题答案: 您正在尝试使用非静态内部类而没有其所属的实例。 非静态内部类必须属于其父类的实例 您可能应该更改为普通类或静态内部类。 或者,您可以编写以创建属于该实例的内部类的实例。

    • 问题内容: 我正在运行一个简单的服务器 当我向主页发出GET请求时,运行时将引发以下错误 我不明白这个错误。有任何想法吗?我正在Cloud9中工作。 我的目录结构是 问题答案: 包含路径是相对的,您需要更新路径以包含“ partials”子文件夹,例如 查看文档

    • null null null 我只是不知道如何将两者结合起来,这样小数点后才是强制性的。 我如何解决这个问题?

    • 我目前正在使用Spring Boot和基于注释的配置来管理我的Spring应用程序。该项目由注册和外部服务库组成。 服务由带有注释的类组成,并在这些类中注入了。 注册将注入。 项目结构(4个maven项目): 注册对服务有maven依赖。 个人服务: PersonRepository: 服务库配置: 应用程序配置: Spring boot在POM中定义用于应用。 我经常遇到的一个例外是,类无法被注

    • 问题内容: Gson用户指南指出,我们应该为任何可以正常使用Gson的类定义默认的无参数构造函数。甚至,在Gson 类的javadoc中说,如果我们尝试对缺少默认构造函数的类实例进行反序列化,则会抛出异常,在这种情况下 应 使用该异常。但是,我尝试对缺少默认构造函数的类使用Gson进行测试,并且序列化和反序列化工作都没有任何麻烦。 这是反序列化的代码。没有非参数构造函数的类: 和一个测试: 效果很