What is a good tool for investigating the use of database connections in Java?

What is a good tool for investigating the use of database connections in Java?

Developers are supporting a complex Java program that is running out of available database connections Since the problem is sporadic, it will be useful to know which thread has opened multiple connections to the database to focus on this area

Finally, the correct fix seems to be rewriting the program to reuse connections, rather than opening multiple connections per thread

I asked what tools a developer could use in his toolbox to investigate the resources allocated by threads, that is, database connections

Solution

Not a specific tool, but a debugging technique for tracking which code is responsible for open connections or other resources

I assume that you use a consistent method in Java to obtain database connections (pooled or unimportant)

The idea is to create a very light wrapper class around the connection factory / pool, or what it is The wrapper will implement the meaning of any JDBC interface, so you can exchange it as a normal connection object, but most methods will only call / return the underlying connection transparently

If you use an IOC framework such as spring, you should be able to easily exchange connection / factory classes at the configuration level Now all Java code will use your new database connection wrapper

If you are using a pool, call connection Close () usually just returns the object to the pool instead of breaking the connection So this technology is suitable for normal connection leakage or just "no return pool (pool exhausted)" leakage

Now we just need to record interesting bits and set a trap for leaking connections

Stack trace to identify Creator

In the constructor or factory method of the connection wrapper, create a new throwable object and store it in the wrapper as a local variable for later use We use throwable because it is better than using thread currentThread(). Getstacktrace() is faster / cheaper

Set traps

Implement the finally method in your wrapper class When an object is destroyed because it is no longer in use, this is a cleanup method called by GC

Should the last method check "I've closed"? If it's closed, everything is fine... But if the connection is connected by GC and hasn't been closed yet, it's a "leaking" connection

Now throwable is back We can catch throwable and output a good log message as follows: "I'm a leaked connection. Here is a stack trace, indicating my creator

Expand ideas

This method can adapt to various situations You can keep other types of data in the wrapper to eliminate your specific problems For example, creation time Then you can poll long-term connections and again hint at the creator Alternatively, you can poll existing connections and parse the throwable stack trace to get data on which code uses how many connections over time

You can probably use existing tools or do these types of things, but in most cases, the amount of code required to apply this technology is very small (suppose you have a simple way to exchange our database connection factory without searching - replacing your entire code base)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>