What method can be used for the investigation of Database Connection usage in Java?
A developer is supporting a complex Java program which is ocassionally exhausting the number of Database connections available. Since the problem is sporadic it would be useful to know which thread has opened multiple connections to the database to focus the effort in this area.
In the end, the correct fix seems to be to rewrite the program to reuse connections and not open multiple connections per thread.
I am asking, what methods should the developer have in his tool box to be able to investigate the resources i.e. Database Connections that have been allocated by a thread.
The close() method of the Connection interface is used to close the connection.
SOLUTION. To resolve this issue, it is necessary to configure a datasource for JDBC that accepts a connection checking mechanism. Before being used, a connection is tested for its validity and not used if it has been closed already. An implementation of either c3p0 or Apache Commons DBCP is recommended.
At the end of your JDBC program, it is required explicitly to close all the connections to the database to end each database session. However, if you forget, Java's garbage collector will close the connection when it cleans up stale objects.
Have a look at log4jdbc. It enables you to have a look at all stuff going over your jdbc, including opening/closing connections as well as connection number information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With