开发者

Enumerate Open JDBC Connections?

开发者 https://www.devze.com 2022-12-16 16:54 出处:网络
How do I enumerate/count all open JDBC connections? I\'ve seen this thread, which doesn\'t answer my qu开发者_JAVA技巧estion.

How do I enumerate/count all open JDBC connections? I've seen this thread, which doesn't answer my qu开发者_JAVA技巧estion.

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.


How do I enumerate/count all open JDBC connections?

If you are not using a wrapper class around DriverManager, then you can't.

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.

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 DriverManager and mock the static method calls and make the required verifications.

0

精彩评论

暂无评论...
验证码 换一张
取 消