使用Spring Boot错误访问两个数据源

时间:2019-02-20 20:54:12

标签: java spring spring-boot jpa jdbc

我正在开发一个可以访问两个基础的APP:带有弹簧启动的Db2和SQL Server。

当我尝试访问第二个数据源时,发生以下错误:

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

驱动程序没有问题,因为当我压缩应用程序使其仅使用其中一个库运行时

请遵循以下项目的状态:

application.properties:

Conexao sqlserver
spring.datasource.url=jdbc:sqlserver://localhost;DatabaseName=DataBaseTest
spring.datasource.username=user
spring.datasource.password=user
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

Conexao db2
spring.secondDatasource.url=jdbc:db2://localhost/DataBaseTest:user=user;password=user
spring.secondDatasource.url.driverClassName = com.ibm.db2.jcc.DB2Driver

多数据库配置

@Configuration
public class MultipleDataSourceConfiguration {

    @Bean
    @Primary
    @ConfigurationProperties("spring.datasource")
    public DataSourceProperties firstDataSourceProperties() {
        return new DataSourceProperties();
    }


    @Bean
    @ConfigurationProperties("spring.secondDatasource")
    public DataSourceProperties secondDataSourceProperties() {
        return new DataSourceProperties();
    }


    @Bean(name="dataSourceFromSQLServer")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public HikariDataSource  dataSourceFromSqlServer() {
        return firstDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }


    @Bean(name="dataSourceFromDb2")
    @ConfigurationProperties("spring.secondDatasource.configuration")
    public HikariDataSource secondDataSource() {
        return secondDataSourceProperties().initializeDataSourceBuilder()
                .type(HikariDataSource.class).build();
    }
}

读者类别

    public class Reader extends JdbcCursorItemReader<BaixaProtesto> {


        @Autowired
        private JdbcTemplate jdbcTemplate;

        private MultipleDataSourceConfiguration multipleDataSourceConfiguration;


        public Reader(DataSource dataSource) {
          //Read base from first datasource
          //everything ok at this point
          //omitted code  
        }


        private BigDecimal getCnpjFromDB2(String name,
                                          int number) {
            //Query ommitted
            final String QUERY = "SELECT ";
            multipleDataSourceConfiguration = new MultipleDataSourceConfiguration();

            //point where the exception is thrown
            HikariDataSource hikariDataSource = multipleDataSourceConfiguration.secondDataSource();
            jdbcTemplate.setDataSource(hikariDataSource);
            return new BigDecimal("0.1");

        }

主班

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

有人知道为什么会出现此错误吗?感谢您的收听

0 个答案:

没有答案