Skip to content

DataSource Configuration HikariCP (New in 0.15.0)

Holger Thurow edited this page Nov 26, 2019 · 2 revisions

HikariCP comes with com.zaxxer.hikari.HikariJNDIFactory, so you can easily hook Hikari's DataSource creation into Simple-JNDI's object creation process. In jndi.properties set

java.naming.factory.object = com.zaxxer.hikari.HikariJNDIFactory

The following shows an example of a DataSource that will be available under the lookup key "application/ds/HikariCP":

In root/application/ds/HikariCP.properties

type = javax.sql.DataSource
dataSource.user = SA
dataSource.password =
dataSourceClassName = org.hsqldb.jdbc.JDBCDataSource
dataSource.databaseName = TestDb/TestDb

The code to obtain the DataSource would be:

InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("application/ds/HikariCP");

This example uses a delimiter of '/', which must be set with the org.osjava.sj.delimiter property.

HikariJNDIFactory and multiple ObjectFactories

There is a problem with HikariJNDIFactory when defining multiple factories as in

java.naming.factory.object = com.zaxxer.hikari.HikariJNDIFactory:spi.objectfactories.DemoBeanFactory

described at https://github.com/brettwooldridge/HikariCP/issues/928. At the time of this writing you have to list HikariJNDIFactory as the last factory in the list. So in the preceding example swap the factories:

java.naming.factory.object = spi.objectfactories.DemoBeanFactory:com.zaxxer.hikari.HikariJNDIFactory

A more stable solution would be to replace HikariJNDIFactory with a subclass returning null when a NamingException is thrown and defining this subclass as HikariCP factory:

java.naming.factory.object = hikari.NullReturningHikariJNDIFactory:spi.objectfactories.DemoBeanFactory

See https://github.com/h-thurow/Simple-JNDI/blob/master/src/test/java/hikari/NullReturningHikariJNDIFactory.java.

As of 0.21.0 there is a more convenient solution for this. See Usage of 3rd party SPI ObjectFactory implementations (New in 0.21.0).