如何将pgproperty与hikaridatasource一起使用?

taor4pac  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(412)

我正在尝试找出如何使用pgproperty属性来配置hikari数据源。
这很好:

final String jdbcUrl = String.format(
            "jdbc:postgresql://%s:%s/%s?ApplicationName=%s",
            configuration.getDatabaseHost(),
            configuration.getDatabasePort(),
            configuration.getDatabaseName(),
            configuration.getDatabaseAppName());
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(configuration.getDatabaseUsername());
hikariDataSource.setPassword(configuration.getDatabasePassword());
hikariDataSource.setMaximumPoolSize(configuration.getDatabaseMaximumPoolSize());

但是我希望能够使用pgproperty来避免像上面那样手工构建jdbc url。当我尝试这个:

hikariDataSource.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
hikariDataSource.addDataSourceProperty(PGProperty.PG_HOST.getName(), configuration.getDatabaseHost());
hikariDataSource.addDataSourceProperty(PGProperty.PG_PORT.getName(), configuration.getDatabasePort());
hikariDataSource.addDataSourceProperty(PGProperty.PG_DBNAME.getName(), configuration.getDatabaseName());
hikariDataSource.addDataSourceProperty(PGProperty.APPLICATION_NAME.getName(), configuration.getDatabaseAppName());
hikariDataSource.addDataSourceProperty(PGProperty.USER.getName(), configuration.getDatabaseUsername());
hikariDataSource.addDataSourceProperty(PGProperty.PASSWORD.getName(), configuration.getDatabasePassword());

我得到这个错误:

Property PGHOST does not exist on target class org.postgresql.ds.PGSimpleDataSource

https://jdbc.postgresql.org/documentation/publicapi/org/postgresql/pgproperty.html

jdg4fx2g

jdg4fx2g1#

hikariDataSource.setJdbcUrl("jdbc:postgresql:");
    hikariDataSource.addDataSourceProperty(PGProperty.PG_HOST.getName(), configuration.getDatabaseHost());
    hikariDataSource.addDataSourceProperty(PGProperty.PG_PORT.getName(), configuration.getDatabasePort());
    hikariDataSource.addDataSourceProperty(PGProperty.PG_DBNAME.getName(), configuration.getDatabaseName());
    hikariDataSource.addDataSourceProperty(PGProperty.APPLICATION_NAME.getName(), configuration.getDatabaseAppName());
    hikariDataSource.addDataSourceProperty(PGProperty.USER.getName(), configuration.getDatabaseUsername());
    hikariDataSource.addDataSourceProperty(PGProperty.PASSWORD.getName(), configuration.getDatabasePassword());
    hikariDataSource.setMaximumPoolSize(configuration.getDatabaseMaximumPoolSize());

相关问题