如何为 postgresql 配置 HikariCP?

How do I configure HikariCP for postgresql?

我正在尝试在 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;

    }

您在 HikariCP configuration 维基页面

中有一个示例
 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);

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: ...

这对我有用:

    HikariConfig config = new HikariConfig();

    config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
    config.addDataSourceProperty("serverName", "xxxxxxxxxxxxxxxxxx");
    config.addDataSourceProperty("portNumber", "xxxx");
    config.addDataSourceProperty("databaseName", "xxxxxxxxxx");
    config.addDataSourceProperty("user", "xxxxxxxxxxxxxxxxxxxxxxxxxx");
    config.addDataSourceProperty("password", "xxxxxxxxxxxxx");

    // postgress configuration for Hikari
    HikariDataSource ds = new HikariDataSource(config);

    return ds;

还要确保你有 Maven 依赖项

<!-- Postgress JDBC Driver -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.10</version>
        </dependency>