Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What method can be used for the investigation of Database Connection usage in Java? [closed]

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.

like image 593
Stuart Woodward Avatar asked Jan 21 '10 07:01

Stuart Woodward


People also ask

Which method is used to close a connection to the database?

The close() method of the Connection interface is used to close the connection.

How do you deal with closed connections in database pool?

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.

Why do we need to close connection with database in Java?

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.


1 Answers

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.

like image 188
bertolami Avatar answered Oct 15 '22 14:10

bertolami