Take a look at http://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html
From the javadoc:
Optionally, one may configure the pool to examine and possibly evict objects
as they sit idle in the pool and to ensure that a minimum number of idle
objects are available. This is performed by an "idle object eviction" thread,
which runs asynchronously. Caution should be used when configuring this
optional feature. Eviction runs contend with client threads for access to
objects in the pool, so if they run too frequently performance issues may
result.
....
minEvictableIdleTimeMillis specifies the minimum amount of time that
an object may sit idle in the pool before it is eligible for eviction
due to idle time. When non-positive, no object will be dropped from
the pool due to idle time alone. This setting has no effect unless
timeBetweenEvictionRunsMillis > 0. The default setting for this
parameter is 30 minutes.
Implement a PoolableObjectFactory
that creates your connections and also implement the PoolableObjectFactory.destroyObject(T object)
method to close your connection. This method will be invoked by the GenericObejctPool when an objects gets evicted.