For various reasons, connections in the pool may become invalid: timeout for connecting to the server, network problems ...
My understanding is that the Tomcat JDBC connection pool does not provide any guarantees as to the validity of the connections it provides to the application.
To prevent (in fact, only reduce the risk) getting an invalid connection from the pool, the solution seems to be a connection check configuration. Testing the connection means starting a very simple query in the database (e.g. SELECT 1; in MySQL).
Tomcat JDBC Connection Pool offers several options for checking connection. I find that more interesting are testOnBorrow and testWhileIdle .
At first, I thought that testOnBorrow is the best option, because it basically checks the connection before providing it to the application (with the maximum frequency determined by validationInterval ).
But after a second, although I realized that testing the connection right before using it can affect the responsiveness of the application. Therefore, I, although using testWhileIdle may be more efficient, as it checks the connections until they are used.
No matter which option I choose, it seems that they only reduce the risk of getting an invalid connection, but this risk still exists.
So I ask: should I use testOnBorrow or testWhileIdle or a combination of both?
On the other hand, I am surprised that validationInterval does not apply to testOnReturn , and I really do not understand the purpose of testOnConnect .