【问题标题】:How do I configure HikariCP for postgresql?如何为 postgresql 配置 HikariCP?
【发布时间】:2018-10-17 05:04:11
【问题描述】:

我正在尝试在 postgresql 中使用 HikariCP,但在任何地方都找不到 postgresql 的配置。

请指出任何带有 HikariCP 的 postgresql 示例或任何相同的配置教程。

我尝试像下面这样使用它,但它不起作用,然后我意识到它是为 MySQL 设计的

public static DataSource getDataSource()

    {

            if(datasource == null)

            {

                    HikariConfig config = new HikariConfig();


            config.setJdbcUrl("jdbc:mysql://localhost/test");

            config.setUsername("root");

            config.setPassword("password");



            config.setMaximumPoolSize(10);

            config.setAutoCommit(false);

            config.addDataSourceProperty("cachePrepStmts", "true");

            config.addDataSourceProperty("prepStmtCacheSize", "250");
            config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");



            datasource = new HikariDataSource(config);

            }

           return datasource;

    }

【问题讨论】:

    标签: database postgresql connection-pooling hikaricp


    【解决方案1】:

    HikariCP configuration wiki 页面中有示例

     Properties props = new Properties();
    
    props.setProperty("dataSourceClassName", "org.postgresql.ds.PGSimpleDataSource");
    props.setProperty("dataSource.user", "test");
    props.setProperty("dataSource.password", "test");
    props.setProperty("dataSource.databaseName", "mydb");
    props.put("dataSource.logWriter", new PrintWriter(System.out));
    
    HikariConfig config = new HikariConfig(props);
    HikariDataSource ds = new HikariDataSource(config);
    

    【讨论】:

    • 主机名在哪里指定?
    • 请问架构怎么样?属性 datasource.hikari.schema 和 DataSource::setSchema 都没有任何效果。 PostgreSQL 似乎使用“当前模式”。有没有办法在 HikariDataSource 中设置它?
    【解决方案2】:

    在 application.yaml 中使用这些设置对我有用:

    javax:
      sql:
        DataSource:
          postgresDataSource:
            dataSourceClassName: org.postgresql.ds.PGSimpleDataSource
            dataSource:
              #url: jdbc:postgresql://localhost:5432/test
              serverName: localhost
              portNumber: 5432
              databaseName: test
              user: ...
              password: ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2018-12-26
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多