相关疑难解决方法(0)

Spring Boot配置和使用两个DataSource

我是Spring和Spring Boot的新手.如何配置和使用两个数据源.

例如,这是第一个数据源的内容.

application.properties

#first db
spring.datasource.url = [url]
spring.datasource.username = [username]
spring.datasource.password = [password]
spring.datasource.driverClassName = oracle.jdbc.OracleDriver

#second db ...
Run Code Online (Sandbox Code Playgroud)

应用类

@SpringBootApplication
public class SampleApplication
{
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

如何修改application.properties以添加其他数据源?如何将其自动装配以供其他仓库使用?

java spring spring-mvc spring-boot

174
推荐指数
6
解决办法
18万
查看次数

从Spring Boot连接到Heroku Postgres

我正在寻找使用JPA/Hibernate 在Spring Boot应用程序中连接到Heroku Postgres最简单,最干净的方法.

我没有在Heroku或Spring Boot文档中看到这个组合的完整示例,所以我想在Stack Overflow上记录这个.

我想尝试这样的事情:

@Configuration   
public class DataSourceConfig {

    Logger log = LoggerFactory.getLogger(getClass());

    @Bean
    @Profile("postgres")
    public DataSource postgresDataSource() {        
        String databaseUrl = System.getenv("DATABASE_URL")
        log.info("Initializing PostgreSQL database: {}", databaseUrl);

        URI dbUri;
        try {
            dbUri = new URI(databaseUrl);
        }
        catch (URISyntaxException e) {
            log.error(String.format("Invalid DATABASE_URL: %s", databaseUrl), e);
            return null;
        }

        String username = dbUri.getUserInfo().split(":")[0];
        String password = dbUri.getUserInfo().split(":")[1];
        String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' 
            + dbUri.getPort() + dbUri.getPath();

        // …
Run Code Online (Sandbox Code Playgroud)

java postgresql spring heroku spring-boot

8
推荐指数
3
解决办法
1万
查看次数

Spring Boot application is ignoring hikaricp config

I'm new to stackoverflow, but read tons of posts here and now stuck.my application.properties is read, but the portion for configuring hikaricp is ignored/has no effect.

I read https://www.javadevjournal.com/spring-boot/spring-boot-hikari/ and folowed those steps there, still any success.

pom.xml

    <dependencies>  
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.4.10.Final</version>
        <exclusions>
            <exclusion>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-jcache</artifactId>
        <version>5.4.10.Final</version>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.6.3</version>
    </dependency>        
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>                
        </exclusions>
        <version>2.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
         <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
          </exclusion>
        </exclusions> …
Run Code Online (Sandbox Code Playgroud)

config spring-boot hikaricp

7
推荐指数
1
解决办法
1万
查看次数

Spring boot + hikari - dataSource or dataSourceClassName or jdbcUrl is required 问题

尝试启动 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
Run Code Online (Sandbox Code Playgroud)

我正在使用 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(); …
Run Code Online (Sandbox Code Playgroud)

java spring datasource spring-boot hikaricp

6
推荐指数
1
解决办法
1万
查看次数

使用 JPA 进行 Sprint 启动 - 创建 bean 名称“entityManagerFactory”时出错 - driverClassName 需要 jdbcUrl

我正在创建一个 Spring Boot 应用程序,它将 JPA 实体保存到 Heroku 上托管的 postgres 数据库中(首先使用 localhost 以加快速度)。几天来我一直在努力弄清楚,所以现在我咬紧牙关寻求帮助。

我的 application.propreties 文件如下所示:

spring.datasource.url=postgres://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypass
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
Run Code Online (Sandbox Code Playgroud)

我收到这些错误:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | …
Run Code Online (Sandbox Code Playgroud)

postgresql entities jpa heroku spring-boot

3
推荐指数
1
解决办法
8057
查看次数

Spring Boot Hikari 配置

我正在为 Spring Boot 应用程序配置 HikariCP,数据库是 Postgresql。

文件说:“我们使用推荐的dataSourceClassName替代jdbcUrl,但无论哪种是可以接受的。”

但是,下一行说:“注意:Spring Boot自动配置用户,需要使用jdbcUrl-based配置。”

如果我们使用jdbcUrl-based 配置并指定dataSourceClassNamethenjdbcUrl将被忽略,如果我们不指定数据源 - HikariDataSource将被创建。所以他们推荐HikariDataSource用于 Spring Boot 应用程序。

如果我们使用dataSourceClassName- 它将使用给定的属性创建(在我的情况下是PGSimpleDataSource使用它的祖先BaseDataSource)。

这两种配置都适合我。

所以,我的问题是:

  1. HikariDataSourcePGSimpleDataSource(或任何其他推荐的)之间有什么区别?
  2. 为什么建议在 Spring Boot 中使用jdbcUrl基于配置(因此HikariDataSource)?

java spring spring-boot hikaricp

2
推荐指数
1
解决办法
4443
查看次数