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
.
Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.
Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat. Retrieve the underlying connection using the javax. sql.
There are multiple JDBC frameworks for connection pooling the most popular choices being Tomcat JDBC and HikariCP.
The default connection pool in hibernate is c3p0 named after the star wars character. But hibernate supports also proxool and used to also advertise apache dbcp. For a while DBCP was dormant and fell out of grace. C3P0 is actually used in production in many projects.
There is no 100% right answer to this. It is a matter of trade-off and context.
But considering this as a corner-case, the testOnBorrow gives pretty good assurance.
Now the trade-off with that is that, every time you request a connection, a query (no matter how light-weight) is made to the database-server. This maybe very fast, but the cost is still not zero.
And if you have a busy application, with very good database-connection-reliability, then you'll start seeing from the data, that the COST of "validity check on every connection-request from the pool" outweighs the benefits of detecting connection issues.
It ensures to the max, that you have a good connection before you use it. Especially considering the cost (retry + manual intervention + loss of workflow etc.) of "not being able to recover easily" from a failed DB operation.
Now imagine if you have the testOnIdle option. This requires that your connections go idle (dependent on the idle timeout of the connection) before a sanity check can be made.
And one last data point is that for some applications, the critical-path is not the "validation query" time (in lower millis hopefully). The applications have bigger issues to deal with. And of course, for some applications, that time is very significant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With