skip to main content
Using the Driver : Statement Pool Monitor : Using JMX to Access the Statement Pool Monitor
  

Try DataDirect Drivers Now

Using JMX to Access the Statement Pool Monitor

Your application cannot access the Statement Pool Monitor using JMX unless the driver registers the Statement Pool Monitor as a JMX MBean. To enable the Statement Pool Monitor as an MBean, statement pooling must be enabled with "MaxPooledStatements", and the Statement Pool Monitor MBean must be registered using the RegisterStatementPoolMonitorMBean connection property.
When the Statement Pool Monitor is enabled, the driver registers a single MBean for each statement pool. The registered MBean name has the following form, where monitor_name is the string returned by the ExtStatementPoolMonitor.getName() method:
com.ddtek.jdbc.type=StatementPoolMonitor,name=monitor_name
Note: Registering the MBean exports a reference to the Statement Pool Monitor. The exported reference can prevent garbage collection on connections if the connections are not properly closed. When garbage collection does not take place on these connections, out of memory errors can occur.
To return information about the statement pool, retrieve the names of all MBeans that are registered with the com.ddtek.jdbc domain and search through the list for the StatementPoolMonitor type attribute. The following code shows how to use the standard JMX API calls to return the state of all active statement pools in the JVM:
private void run(String[] args) {
   if (args.length < 2) {
      System.out.println("Not enough arguments supplied");
      System.out.println("Usage: " + "ShowStatementPoolInfo hostname port");
   }
   String hostname = args[0];
   String port = args[1];
   JMXServiceURL          url = null;
   JMXConnector            connector = null;
   MBeanServerConnection   server = null;
   try {
      url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" +
                              hostname +":" + port + "/jmxrmi");
      connector = JMXConnectorFactory.connect(url);
      server = connector.getMBeanServerConnection();
      System.out.println("Connected to JMX MBean Server at " +
                        args[0] + ":" + args[1]);
      // Get the MBeans that have been registered with the
      // com.ddtek.jdbc domain.
      ObjectName ddMBeans = new ObjectName("com.ddtek.jdbc:*");
      Set<ObjectName> mbeans = server.queryNames(ddMBeans, null);
      // For each statement pool monitor MBean, display statistics and
      // contents of the statement pool monitored by that MBean
      for (ObjectName name: mbeans) {
         if (name.getDomain().equals("com.ddtek.jdbc") &&
             name.getKeyProperty("type")
                .equals("StatementPoolMonitor")) {
            System.out.println("Statement Pool - " +
               server.getAttribute(name, "Name"));
            System.out.println("Max Size:    " +
               server.getAttribute(name, "MaxSize"));
            System.out.println("Current Size: " +
               server.getAttribute(name, "CurrentSize"));
            System.out.println("Hit Count:    " +
               server.getAttribute(name, "HitCount"));
            System.out.println("Miss Count:   " +
               server.getAttribute(name, "MissCount"));
            System.out.println("Statements:");
            Object[] params = new Object[3];
            params[0] = new Integer(-1);
            params[1] = new Integer(-1);
            params[2] = new Integer(-1);
            String[] types = new String[3];
            types[0] = "int";
            types[1] = "int";
            types[2] = "int";
            ArrayList<String>statements = (ArrayList<String>)
               server.invoke(name,
                            "poolEntries",
                            params,
                            types);
            for (String stmt : statements) {
               int index = stmt.indexOf(";");
               System.out.println("   " + stmt.substring(0, index));
            }
         }
      }
   }   
   catch (Throwable except) {
      System.out.println("ERROR: " + except);
   }
}