【问题标题】:Spring Boot - multiple datasource configurationSpring Boot - 多数据源配置
【发布时间】:2021-03-16 16:01:42
【问题描述】:

我的应用程序中有以下 application.properties 用于配置两个数据源,我的 Spring boot 版本是 2.1.3.RELEASE

spring.datasource2.url=jdbc:postgresql://localhost:5432/db1
spring.datasource2.username=//username
spring.datasource2.password=//pwd

spring.datasource1.url=jdbc:postgresql://localhost:5432/db2
spring.datasource1.username=//username
spring.datasource1.password=//pwd
spring.datasource1.initialization-mode=always

spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.h2.console.enabled=true

我收到以下错误

ContextLoader:296 - Root WebApplicationContext: initialization completed in 3970 ms
 [main] WARN  AnnotationConfigServletWebServerApplicationContext:557 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-12-04 20:47:27 [main] INFO  StandardService:173 - Stopping service [Tomcat]
2020-12-04 20:47:27 [main] INFO  ConditionEvaluationReportLoggingListener:142 - 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-04 20:47:27 [main] ERROR LoggingFailureAnalysisReporter:42 - 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

到目前为止我尝试了什么:

 Disabling DatasourceAutoConfiguration.class, HibernateJPAAutoCOnfiguration.class, TransactionManagerAutoConfiguration.class

没有用。

我该如何解决这个问题?

提前谢谢你!

【问题讨论】:

    标签: spring-boot hibernate spring-data-jpa spring-data


    【解决方案1】:

    正如我们在错误中看到的,spring 需要 url。在Spring boot初始化的时候,spring会去寻找这个属性spring.datasource.url=jdbc:postgresql://localhost:5432/postgres

    您使用了一些 Spring Boot 属性中不存在的属性。

    spring.datasource2.url=jdbc:postgresql://localhost:5432/db1
    spring.datasource2.username=//username
    spring.datasource2.password=//pwd
    
    spring.datasource1.url=jdbc:postgresql://localhost:5432/db2
    spring.datasource1.username=//username
    spring.datasource1.password=//pwd
    

    如果要使用多个数据源,则必须以编程方式创建。

    您可以参考以下链接:https://www.baeldung.com/spring-boot-configure-data-source-programmatic

    https://www.baeldung.com/spring-data-jpa-multiple-databases

    【讨论】: