The easiest is to configure the connection pool to specify the query to be run to test the connection before it is passed to the application:
validationQuery="select 1 as dbcp_connection_test"
testOnBorrow=true
This same “connection validation” query can be run on other events. I’m not sure of the defaults for these:
testOnReturn=true
testWhileIdle=true
There are also configuration settings that limit the “age” of idle connections in the pool, which can be useful if idle connections are being closed at the server end.
minEvictableIdleTimeMillis
timeBetweenEvictionRunsMillis