I had long running queries in production because of which connections getting closed/abandoned. As a result I updated removeAbandonedTimeout to 1800 sec. After this, 95% of the issue is gone. But very randomly, once or twice a month I am still getting connection abandoned errors.
The problem here is retrying after a wait time of 15 sec after catching this exception is not helping resolve the issue. The subsequent queries are immediately failing with connection abandoned exception.
My current pool configuration:
maxActive=50;
maxIdle=1;
minIdle=0;
initialSize=0;
maxWait=10000;
testOnBorrow=true;
testOnReturn=false;
timeBetweenEvictionRunsMillis=5000;
minEvictableIdleTimeMillis=2000;
testWhileIdle=false;
testOnConnect=false;
validationQuery=SELECT 1;
validationQueryTimeout=-1;
validationInterval=30000;
removeAbandoned=true;
removeAbandonedTimeout=1800;
logAbandoned=true;
jdbcInterceptors=ConnectionState;StatementFinalizer;ResetAbandonedTimer;
Replication
Database used: PostgreSQL
I changed removeAbandonedTimeout
to 30sec
. The first pg_sleep failed as it exceeded timeout and entered catch block. My expectation was, since the connection is removed by background abandonedTimer, the next pg_sleep(10) should request a new connection and should get executed successfully as testOnBorrow
is true. So I tried sleeping the current thread for 30 sec and executed the next pg_sleep. None of my catch or finally block statements are executing.
try {
jdbcTemplate.execute("select pg_sleep(40)");
} catch(DataAccessException e) {
LOGGER.info(e.getMessage(), e);
Thread.sleep(30000);
jdbcTemplate.execute("select pg_sleep(10)");
LOGGER.info("Executed fall back case");
} finally {
jdbcTemplate.execute("select 1");
}