2

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.

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

in the source code, if lastAccess near than 1000ms, the alive check will not be done : github.com/brettwooldridge/HikariCP/blob/dev/src/main/java/com/…
@Chinaxing a good observation, my interpretation is that this is performance optimization based on the assumption, that in a short idle time the connection will not get broken. This could be sensitive approach. Anyway the interval was changed recently
0

Use con.close in finally block, otherwise in the catch block you must ask first if con != null for avoid the exception produced in the catch block. Other alernative could be enclose, con.close() in other try{}catch{} block. Hope this help.

Comments

0

You should consider try-with-resource:

try(Connection con = pool.getConnection()) { DO SOMETHING WITH CONNECTION }catch(Exception e){ e.printStackTrace(); } 

this will guarantee the connection is given back to the pool. Checking if a connection is valid is a task for the pool, not for your code. You can configure the pool to check your connection with the connectionTestQuery property - so there's that.

1 Comment

Not every Exception will break the Cinnection. So forcing reconnect on non fatal error will be more expensive.
0

If you are using try-with-resource and if network is broken, HikariCP most likely will close connection, not recycle. from HikariCP source, if there is such exception in connection.close(), it is closing connection in checkException()

2 Comments

the close function maybe not do network related task. so such exception maybe not threw, and the checkException will not be called. and the connection will be recycled to the pool
better write test case that reproduces the behavior you imagine would be interesting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.