I thought a bit, and decided to leave this question to be, though problem was not in Tomcat DBCP at all. My decision is motivated by the fact that I found quite a number of similar questions, and many of them unanswered, and never found a clue for what might have been going on. So, I leave a message here just as a warning for anyone suddenly walking the same path:
The problem was that I had a resource using database which was initialized just once, like:
class MyClass {
private MyResource res = null;
private MyResource getMyResource() {
if (res == null) res = new MyResource(getConnection());
return res;
}
private Connection getConnection() {
....
con = dataSource.getConnection();
....
return con;
}
}
When my app was in Servlet it led to the situation where instance of MyResource were the only one, and had connection open. So, when MySQL server kill the expired connection my app would get exception about timeout on first query, but owing to autoReconnect for subsequent queries connection will be alive again. Pool just didn't touched connection because it had never been released.
Finally I realized it, and fixed by changing getMyResource
to:
private MyResource getMyResource() {
return new MyResource(getConnection());
}
In my case it was reasonable. After this change all things started to work as expected.