Error access two datasource with Spring Boot

我的未来我决定 提交于 2021-01-29 09:02:27

问题


I'm developing an APP that accesses two bases: Db2 and SQL Server with spring boot.

When I try to access the second datasource the following error occurs:

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

It is not a problem with the driver because when I squeeze the app to run with just one of the banks it works

Follow below how the project is:

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

Multi database configuration

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

Class of reader

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

        }

Main class

@SpringBootApplication
public class Application {

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

Does anyone know why this error? Thanks for listening

来源:https://stackoverflow.com/questions/54795090/error-access-two-datasource-with-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!