For various reasons connections in a pool can become invalid: server connection timeout, network issues...
My understanding is that a Tomcat JDBC Connection Pool does not provide any guaranty about the validity of the connections it provides to the application.
To prevent (actually only lower the risk) getting an invalid connection from the pool a solution seems to be the configuration of connections validation. Validating a connection means to run a very basic query on the database (e.g. SELECT 1;
on MySQL).
Tomcat JDBC Connection Pool offers several options to test the connection. The two I find the more interesting are testOnBorrow
and testWhileIdle
.
First I was thinking that testOnBorrow
is the best option because it basically validate the connection before providing it to the application (with a max frequency defined by validationInterval
).
But after a second though I realized that testing the connection right before using it might impact the responsiveness of the application. So I though that using testWhileIdle
can be more efficient as it test connections while they are not used.
No matter which option I choose it seems that they only lower the risk from getting an invalid connection but this risk still exist.
So I end up asking: should I use testOnBorrow
or testWhileIdle
or a mix of both?
On a side note, I'm surprised that validationInterval
does not apply to testOnReturn
and I don't really get the purpose of testOnConnect
.