简体   繁体   中英

DB connection pool expired, then what?

After my connection pool expires, when I try to open more connections in parallel than my max number of allowed connections in the pool, then I start getting timeout exceptions when trying to obtain a connection from the pool.

That is expected, however, the pool seems to be left in that state, where everything else I do since that moment gets the same timeout exceptions. As if each of the connections in the pool had been left busy, and can't be reused. I would expect the connections to get freed up over time, and then other connections being allowed, but this is not happening.

I'm using Play 1.2.5 with a jdbc driver to mysql, and from the logs I reckon the pool is C3P0.

I'm not explicitly closing the connections, since I believe is the right thing to do when using a pool, but I'm not 100% sure.

I don't know if this could be a connection leak in one of the framework/libraries I'm using, or if I'm doing something wrong or not doing something I should.

When I catch one of the timeout exceptions, what is the right thing to do?

You must explicitly close connections when you use a connection pool. A connection pool has a collection of physical connections to a database. When you request a connection from the pool, it marks that physical connection as in-use and hands you a logical handle to that connection. This logical handle essentially is a wrapper or proxy which forwards most method calls (either directly or with some modification) to the physical connection.

When you call close() on this logical handle, the connection pool gets a signal that the physical connection is available again (that is: can be returned to the pool), the logical handle will from then on behave as a closed connection, but the actual physical connection is still open. If you don't call close() , the connection pool never gets this signal so the physical connection will remain in-use and won't be available for re-use.

Some advanced pool configurations allows the pool to detect this situation (eg using a timeout, or maybe with finalizers etc) and reclaim the connection, but you should not depend on that.

TL;DR: Always call close() on your connection when you are done with it, whether it comes from a connection pool, a non-pooled DataSource or DriverManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM