HikariPool$PoolInitializationException is not an SQLException, intended?

136 views
Skip to first unread message

Kevin Schmidt

unread,
Feb 15, 2017, 4:54:07 PM2/15/17
to HikariCP

Using 2.5.1 with MySQL, if the JDBC URL is configured incorrectly pointing to a non-existent database/schema, the MySQL driver throws a MySQLSyntaxErrorException which is a SQLException. HikariCP wraps this in a HikariPool$PoolInitializationException which is not a SQLException.


This means that code I have that calls DataSource.getConnection() and catches SQLException that works just fine using MySQL directly fails when using HikariCP as the exception is not caught.


Was there a reason HikariCP's pool init exception isn't a SQLException? Or should it be for this reason?

Brett Wooldridge

unread,
Feb 15, 2017, 8:08:15 PM2/15/17
to HikariCP
Hi Kevin,

It is a bit complicated, maybe we can do better but...  currently pool initialization occurs in one of two ways.

One: When the HikariDataSource is constructed.

HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("user");
config.setPassword("password");

HikariDataSource dataSource = new HikariDataSource(config);

In this case, the pool is initialized at the moment of HikariDataSource construction -- we have all of the configuration required to start the pool.  Because constructors typically do not declare exceptions, an unchecked (Runtime) exception (PoolInitializationException) is thrown.


Two: When HikariDataSource.getConnection() is called.

HikariDataSource dataSource = new HikariDataSource();
dataSource.setJdbcUrl();
dataSource.setUsername("user");
dataSource.setPassword("password");

Connection connection = dataSource.getConnection();

In this case, when HikariConfig is not used and instead the HikariDataSource is configured directly, we do not know that configuration is complete until the user calls getConnection().  So, at the point of calling getConnection() the pool initialization is performed.  Because the initialization logic is the same, the same PoolInitializationException is thrown.  Note however, that calling getCause() on the PoolInitializationException should return the underlying database exception (in your case, MySQLSyntaxErrorException).

We could possibly catch PoolInitilizationException internally in this second case, and unwrap and throw the original exception...

Brett

Kevin Schmidt

unread,
Feb 15, 2017, 10:24:36 PM2/15/17
to HikariCP
Brett,

Thanks for the explanation.  It does make sense, but I for one would vote for what you suggest could be done, to catch and unwrap the SQLException in the second case.  Otherwise, clients are required to have knowledge that a pool is being used and catch RuntimeExceptions where normal JDBC use would only require normal SQLException handling.

Kevin
Reply all
Reply to author
Forward
0 new messages