How do I enumerate/count all open JDBC connections? I’ve seen this thread, which doesn’t answer my question.
My interest is in writing test code in which the object being tested (call it ConnectionUser) maintains its own connection. In my tests, I want to do things like verify that instantiating ConnectionUser doesn’t open a connection, and that calling ConnectionUser.open results in one open connections. I’m not interested in enumerating/counting connections in the production code itself or implementing a connection pool.
I’ve thought about using something like jmockit to verify invocations of the static DriverManager.getConnection method, but would prefer something less abstract – i.e, have the driver just give me a list of all open connections.
If you are not using a wrapper class around
DriverManager, then you can’t.Maybe… but the driver won’t give you that information. Actually, if you want to test your code in isolation, then using mocks is the way to go. After all, how do you know the driver is not lying to you 🙂 So, indeed, I’d use JMockit to replace the
DriverManagerand mock the static method calls and make the required verifications.