I have a jdbc connection pool using the KeyedObjectPool class and i use it via the implemented methods openConnection()
and closeConnection()
. closeConnection()
normally just returns the connection to the pool and closes it only if it has been used to often before. Therefore it uses ConnectionPool.returnObject()
which does NOT close the connection object (see here).
class ConnectionPool
{
public openConnection(...)
{
...
}
public closeConnection()
{
...
}
}
However, if i use the connection pool like this:
try (Connection connection = sConnectionPool.openConnection(JdbcCredentials);)
{
doSomething();
}
catch (Exception e)
{
...
}
Is the connection object using try-with
only returned to the pool or actually closed with connection.close()
(which i don't want to happen because it would make my pool useless)?
close()
on a pooled connection returns it to the pool, regardless of whether that was caused by thetry
or your own code.Otherwise it wouldn't be reusable, so it wouldn't be a connection pool.