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?
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>");
}
}