3

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)
daferrecpr
  • 33
  • 5
  • I believe it should be `"databaseName", "[REDACTED]"` not `"database", "[REDACTED]"`. – andrewJames May 28 '22 at 00:04
  • See also the examples here: [How do I configure HikariCP for postgresql?](https://stackoverflow.com/questions/50213381/how-do-i-configure-hikaricp-for-postgresql) – andrewJames May 28 '22 at 00:04
  • Hello @andrewJames, is there a comprehensive list of property names somewhere? Sorry if this sounds idiotic. – daferrecpr May 28 '22 at 00:28
  • See the HikariCP web site for all the HikariCP settings. And see the relevant Postgres JDBC documentation for all the Postgres JDBC settings. – andrewJames May 28 '22 at 00:56
  • I've seen both sites, and sometimes the names don't correspond as in the case above - the Postgres JDBC driver says "database" is the name of the database name parameter. – daferrecpr May 28 '22 at 02:36
  • Perhaps it would be easier to configure an object of type `PGSimpleDataSource` first. After that is done, pass to [`HikariConfig#setDataSource`](https://javadoc.io/static/com.zaxxer/HikariCP/5.0.1/com/zaxxer/hikari/HikariConfig.html#setDataSource(javax.sql.DataSource)). – Basil Bourque May 28 '22 at 03:40
  • @BasilBourque Thanks for the suggestion. Didn't realize that was an option, and seems less ambiguous than the direct approach of adding properties using `addDataSourceProperty("something", "the_other_thing")`. I'll use that in the future. – daferrecpr May 28 '22 at 04:29
  • 1
    @daferrecpr If my suggested approach works out, please come back here to draft, post, and accept an Answer to your own Question, for posterity. – Basil Bourque May 28 '22 at 07:09

1 Answers1

2

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197