Home > Software design >  Why does HikariCP say "Property database does not exist on target class org.postgresql.ds.PGSim
Why does HikariCP say "Property database does not exist on target class org.postgresql.ds.PGSim

Time:05-29

I am attempting to set up various PostgreSQL JDBC driver properties to my HikariCP pool, but for some reason, it's stating that those properties don't exist. Why so? Am I using the wrong parameter names?

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();
    }
}

Output:

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)

CodePudding user response:

It gives that error because PGSimpleDataSource does not have a property database (i.e. it doesn't have a setDatabase(String) method). It does have a property databaseName (setDatabaseName defined in BaseDataSource). This property is specified in section 9.6.1 DataSource Properties of the JDBC 4.3 specification.

Reading the comments, it looks like you're confusing the documentation of the JDBC URL format (and connection properties) with the properties that are available on the data source implementations provided by the driver. To be clear, that documentation doesn't specify there is a property database, it only uses database as a placeholder in the JDBC URL syntax (as in jdbc:postgresql://host/database.

  • Related