Database Connection Leak and Connection Protector

Connection leakage. Too many open connections. Did you face it in some legacy code maybe? If yes, you probably know how hard is to find where is the leakage. Especially when you have some additional aspects in your code that "should" work, but there is no way to figure it out. Is there a way to monitor connection leakage? Is there a way to execute some code and cleanup connections if code is clunky?

It looks like we can use some hack here, that will work in any, especially legacy environment. We can use Mockit to inject our code into legacy connection pool. Gather all active connections and manage them.

We can also try to execute our code in special context where we can discover where is our nasty leakage.

Secured context execution can be used in tests, to execute "suspicous" code and see how many connections we have. We can also wrap each test case execution and assert open connections size. What do you think about this?