wwadge/bonecp — pull request #28 by jinpeng007 (2014-09-16 04:03:40+00:00)
Title: Patch idle connection

Hey BoneCP Folks,

Here is a patch based on the 0.8.0 release to address the connection pool size overflow issues.

We've identified two problems while using BoneCP in our product:
1. The connectionCreated counter can go into negative value during network failures between the application nodes and the database servers. Once the value is negative, the BoneCP PoolWatchThread always try to keep 2*maxConnectionsPerPartition connections for each partition.
2. In the ConnectionTesterThread, when the connection's idle time > the idleMaxAgeInSeconds, the connection is killed, but immediately a new connection is created in the pool. Due to this logic, once the connection pool gets to the full size maxConnectionsPerPartition, it will stay full forever.

![bonecpconnectionoverflow](https://cloud.githubusercontent.com/assets/8782012/4282322/41ae8ea8-3d56-11e4-90d8-801537dc861f.png)

We use BoneCP in a distributed environment, the number of connections to database servers from each application node can significantly impact the scalability and robustness of the system. We want to keep the number connections from each node as the minimum all the time: only increase the pool size on demand; after peak time usage, let the idle timeout logic kick in to shrink the pool size to minimum.

The patch provided two fixes:

1. Make sure connectionCreated >= 0.
2. Let idle connections die off the connection pool after idleMaxAgeInSeconds, therefore allow the pool size to shrink.