为什么HikariCP说“Property database does not exist on target class org.postgresql.ds.PGSimpleDataSource”?

e7arh2l6  于 2023-05-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(369)

我试图设置各种PostgreSQL JDBC驱动程序properties到我的HikariCP池,但由于某种原因,它说这些属性不存在。为什么会这样?我是否使用了错误的参数名?

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource

import java.sql.Connection;
import java.sql.SQLException;

public class HikariTest {
    public static void main(String[] args) throws SQLException {
        HikariConfig config = new HikariConfig();
        config.setDataSourceClassName("org.postgresql.ds.PGSimpleDataSource");
        config.setUsername("[REDACTED]");
        config.setPassword("[REDACTED]");
        config.addDataSourceProperty("host", "[REDACTED");
        config.addDataSourceProperty("database", "[REDACTED]");
        config.addDataSourceProperty("ssl", true);
        config.addDataSourceProperty("sslcert", "[REDACTED]");
        HikariDataSource ds = new HikariDataSource(config);
        Connection conn = ds.getConnection();
    }
}

输出:

Exception in thread "main" java.lang.RuntimeException: Property database does not exist on target class org.postgresql.ds.PGSimpleDataSource
    at com.zaxxer.hikari.util.PropertyElf.setProperty(PropertyElf.java:127)
    at com.zaxxer.hikari.util.PropertyElf.lambda$setTargetFromProperties$0(PropertyElf.java:51)
    at java.base/java.util.concurrent.ConcurrentHashMap.forEach(ConcurrentHashMap.java:1603)
    at java.base/java.util.Properties.forEach(Properties.java:1422)
    at com.zaxxer.hikari.util.PropertyElf.setTargetFromProperties(PropertyElf.java:46)
    at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:323)
    at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:112)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:93)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
    at HikariTest.main(HikariTest.java:21)
zdwk9cvp

zdwk9cvp1#

它给出该错误是因为PGSimpleDataSource没有database属性(即它没有setDatabase(String)方法)。它有一个属性databaseNamesetDatabaseName定义在BaseDataSource中)。此属性在JDBC 4.3规范的 9.6.1 DataSource Properties 节中指定。
阅读这些注解,您似乎混淆了JDBC URL format(和连接属性)的文档与驱动程序提供的数据源实现中可用的属性。需要说明的是,该文档没有指定database属性,它只使用database作为JDBC URL语法中的占位符(如jdbc:postgresql://host/*database)。

相关问题