Wednesday, April 20, 2011

Database Connection Pooling and too may TCP connetions in TIME_WAIT state

TCP TIME_WAIT is the final state and usually it will stay there for 4 minutes. Detailed explanations of TCP TIME_WAIT could be found here:

http://stackoverflow.com/questions/41602/how-to-forcibly-close-a-socket-in-time-wait
http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html

Recently, I experienced a problem with too many connections in TIME_WAIT state, which prevented new connection from creating on the server side. I used a database connection pool, i.e., common dbcp, to pool the connections. I was very surprised to see so many connections are created and closed. What was the problem? Finally, I realized that I did not set the connection pool correct. The connection pool has parameters such as max connections, max idle connections, and wait time. For my case, the number of max idle connections is too low and thus, the connection pool had to create a lot of new connections. Once I increased the max idle connections, the problem went away. A lesson to lean.

1 comments: