Search code examples
jdbcconnection-poolinghikaricp

dataSourcePool close broken connection when release to pool


I'm use Hikari jdbc connection pool.

when execute statement witch produce an exception ( for example by network is broken). as following:

try{
   Connection con = pool.getConnection();
   con.executeQuery("....");
}catch(Exception e){
   con.close();
}

does con.close will evict the broken connection, instead of release it to the pool.

if the broken connection is released to pool. it maybe got by following request of getConnection.


Solution

  • If your driver is JDBC4 compatible (and supports Connection.isValid()) the validation of the connection is done via this API per default).

    Otherwise you may set the connectionTestQuery to be performed for the validation.

    See also this discussion and the source code.