monitor JDBC connections
Asked Answered
H

2

10

I am trying to monitor JDBC connections in tomcat using JMX.

But it is giving information only regarding the Datasource resources, I want the status of connection defined in Database.properties file.

Is there any way to get their status?

Hearthside answered 27/12, 2012 at 8:39 Comment(3)
I just want the number of busy and idle threads not the waiting or block status.Hearthside
What is Database.properties? How is it related to Tomcat, what framework is this?Bally
Hi Rupinder, did you finally find such a utility?Multifarious
C
5

With this Servlet 3.0 example you can monitor all information in the org.apache.tomcat.jdbc.pool.jmx.ConnectionPool including the Idle and Active connections.

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.util.Set;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/poolmonitor")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("<!DOCTYPE html>");
        writer.println("<html>");
        writer.println("<body>");
        writer.println("<p><h1>Tomcat Pool</h1></p><p>");
        try {
            MBeanServer server = ManagementFactory.getPlatformMBeanServer();
            Set<ObjectName> objectNames = server.queryNames(null, null);
            for (ObjectName name : objectNames) {
                MBeanInfo info = server.getMBeanInfo(name);
                if (info.getClassName().equals(
                        "org.apache.tomcat.jdbc.pool.jmx.ConnectionPool")) {
                    for (MBeanAttributeInfo mf : info.getAttributes()) {
                        Object attributeValue = server.getAttribute(name,
                                mf.getName());
                        if (attributeValue != null) {
                            writer.println("" + mf.getName() + " : "
                                    + attributeValue.toString() + "<br/>");

                        }
                    }
                    break;
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        writer.println("</p></body>");
        writer.println("</html>");
    }
}
Candycecandystriped answered 11/7, 2017 at 7:3 Comment(0)
S
4

You are asking for more than JNDI DataSource information (such as busy and idle threads).

I highly recommend this resource for JMX-based monitoring:

https://cwiki.apache.org/confluence/display/TOMCAT/Monitoring

Shoring answered 27/12, 2012 at 22:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.