HikariCP and H2 datasource

9,178 views
Skip to first unread message

Luis Ángel Vicente Sánchez

unread,
Dec 19, 2013, 9:11:07 PM12/19/13
to hika...@googlegroups.com
I have tried to use HikariCP with H2 database, the database we use for unit tests, and it didn't work. If you check the DataSource class, org.h2.jdbcx.JdbcDataSource, you will see that the method to set the connection URL is setURL instead of setUrl. Would it be possible to modify HikariCP to make it work with H2? I have already send an email to H2 google group to check if they could rename that method or provide an alias to make their datasource consistent with other datasources.

I have also discovered that HikariDataSource has an inner pool, HikariPool, that is only accessible to package members; I wanted to log things like ActiveConnections, IdleConnections, ... is there anyway I can access those values?

Regards,

Luis

Brett Wooldridge

unread,
Dec 20, 2013, 3:29:00 AM12/20/13
to
Hi Luis,

Thanks for trying HikariCP.  I have no trouble connecting to a H2 database.  I set my pool up like the following example:

HikariConfig config = new HikariConfig();

config.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");

config.setConnectionTestQuery("VALUES 1");

config.addDataSourceProperty("URL""jdbc:h2:~/test");

config.addDataSourceProperty("user", "sa");

config.addDataSourceProperty("password", "sa");

HikariDataSource ds = new HikariDataSource(config);


I had no trouble connecting with this configuration.  Basically, Hikari will append "set" to whatever property name you give, so "URL" becomes "setURL" when invoked.  Any method on a DataSource that follows the "setXYX()" convention for method names can be invoked by Hikari.  This is pretty standard Java practice, and I have yet to find a DataSource it doesn't work with.


With regard to internal pool statistics, there is a JMX MBean available that exposes:


* Idle Connection count

* Active Connections (in use)

* Total Connections

* The number of threads waiting for a connection


You can see these values through a JMX console like JConsole.  Or you can lookup the MBean yourself.


The MBean is registered with the name:


com.zaxxer.hikari:type=Pool (<poolName>)


where poolName is a name you can set on the HikariConfig using config.setPoolName("foo"), for example.


Off the top of my head, you can do something like:


       MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

       ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (foo)");

       Integer idleConnections = (Integer) mBeanServer.getAttribute(poolName, "IdleConnections");


The accessible attributes are: IdleConnections, ActiveConnections, TotalConnections, ThreadsAwaitingConnection.

Brett

Luis Ángel Vicente Sánchez

unread,
Dec 20, 2013, 6:29:21 AM12/20/13
to hika...@googlegroups.com
Thank you! I don't know why I didn't try that first. Now I'm getting this warning message:

[info] 11:25:50.271 [main] WARN  c.z.h.javassist.HikariClassScanner - DataSource org.h2.jdbcx.JdbcDataSourcere not found in the instrumentation codex.  Please report at http://github.com/brettwooldridge/HikariCP.

But everything work as expected.

Brett Wooldridge

unread,
Dec 20, 2013, 7:42:21 AM12/20/13
to
Good, I'm glad it's working.  The warning is harmless.  HikariCP supports what is called
instrumentation mode for a slight performance boost (about 30%), but it is only supported
for certain drivers.  That warning is there so new and/or unknown JDBC drivers can be
discovered and support added.  I should probably make that an "INFO" level message
rather than a "WARN" so it doesn't alarm users.

I'll add instrumentation support for the H2 driver in the next few weeks.

Brett

Reply all
Reply to author
Forward
0 new messages