Spring Boot + Hikari-dataSource或dataSourceClassName或jdbcUrl是必需的问题

时间:2018-11-24 05:24:39

标签: java spring spring-boot datasource

尝试启动我的Spring应用程序时出现以下错误

ERROR 5908 --- [ main] com.zaxxer.hikari.HikariConfig : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

我的application.properties文件如下:

spring.datasource.one.jdbc-url = jdbc:postgresql://10.x.x.x:y/sampledb1
spring.datasource.one.username = someuser
spring.datasource.one.password = somepasswd
spring.datasource.one.driver-class-name = org.postgresql.Driver

spring.datasource.two.jdbc-url = jdbc:postgresql://10.x.x.x:z/sampledb2
spring.datasource.two.username = someuser
spring.datasource.two.password = somepassword
spring.datasource.two.driver-class-name = org.postgresql.Driver

我正在使用DataSourceBuilder类,如下所示:

@Configuration
public class DataSourceConfig
{
    @Bean(name = "one")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.one") 
    public DataSource dataSource1()
    {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "two")
    @ConfigurationProperties(prefix = "spring.datasource.two") 
    public DataSource dataSource2()
    {
        return DataSourceBuilder.create().build();
    }
}

我的pom看起来像这样。

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <avro.version>1.8.2</avro.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <version.powermock>1.6.2</version.powermock>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>log4j-over-slf4j</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- hystrix -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
     </dependencies> 

此功能之前工作正常,但现在引起了一些问题。并且错误发生intermittently,有时它开始时没有错误,而其他时候却由于错误而失败。

我尝试了link中建议的解决方案。它们似乎对我不起作用。

1 个答案:

答案 0 :(得分:0)

jdbc-url更改为jdbcUrl,以便Hikari可以为每个URL找到合适的驱动程序。

  

jdbcUrl   此属性指示HikariCP使用“基于DriverManager的”配置。我们认为基于数据源的配置(上文)优于多种原因(请参见下文),但是对于许多部署而言,差异不大。当将此属性与“旧”驱动程序一起使用时,您可能还需要设置driverClassName属性,但首先尝试不使用该属性。请注意,如果使用此属性,则仍可以使用DataSource属性来配置驱动程序,实际上,建议您对URL本身中指定的驱动程序参数进行推荐。默认值:无