Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monitor JDBC connections

Tags:

tomcat

jdbc

jmx

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?

like image 410
Rupinder Avatar asked Dec 27 '12 08:12

Rupinder


People also ask

How to monitor JDBC connection?

From the JDBC Connection Pool—>Monitoring tab, you can view information about the state of each deployed instance of the selected connection pool. That is, for each server on which the connection pool is deployed, you can see current status information about the connection pool.

How to check JDBC connection pool?

In the JDBC Connection Pool—>Testing tab, you can test a JDBC connection in a connection pool on each server on which the connection pool is deployed. When you test a connection pool, WebLogic Server reserves and releases a connection from the connection pool.


2 Answers

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>");
    }
}
like image 62
PbxMan Avatar answered Sep 18 '22 13:09

PbxMan


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

like image 20
Christopher Schultz Avatar answered Sep 18 '22 13:09

Christopher Schultz