Connection Leak

A situation which occurs when a connection is opened, then forgotten about. This is known as a "leak", because each time it happens, one less connection is available for re-use.

The most common type of Connection Leak experienced in Java development, is when using a Connection Pool (such as DBCP). If there is an area of code which does not close the connection properly, every time that area of code is executed, a connection will "leak" from the pool. After all the available connections have been leaked, there will be no more available, and the application will hang.


  • The application asks the pool for a connection, uses it to talk to the database, then returns it to the pool.
  • There is typically a setting such as maxActive which limits the number of connections which the pool will hold.
  • If maxActive=10, then the pool can be asked for up to 10 connections at the same time. (eg: 10 concurrent users). The application is expected to return the connections to the pool after completing the user's request.
  • If the application never returns the connections, after these 10 run out, the pool has none left. In this case, a new caller will wait for a connection to become available. This makes it appear to have hung.

How can I stop Connection Leaks?

  • Close connections, pay special attention to any error handling code, as it will be called less often and will be therefore harder to find if you have problems later.
  • (Added by Anonymous user) Here's a tool that helps:

How can I stop my application hanging if there are no more connections?

  • Do not simply increase the maxActive attribute. This just hides the problem, making it harder to detect, and wastes server resources.
  • Use the maxWait attribute in your connection pool (eg: DBCP), to limit the wait time to 5-10 seconds. After this time elapses, the application will show an error instead of hanging. This is usually preferable, as it is logged or can trigger an external error handling procedure.
  • Use removeAbandoned, removeAbandonedTimeout and logAbandoned (if supported). This allows the pool to time the usage of a connection, and forcibly reclaim it. Using this method adds overhead to the connection time, but may be useful temporarily while debugging.

See Also

metawerx specific

referring pages