10 Statement Pool Monitor : Accessing the Statement Pool Monitor

Accessing the Statement Pool Monitor
Your application cannot access the Statement Pool Monitor unless you enable statement pooling. See “Enabling Statement Pooling” for more information.
Applications and tools can access the Statement Pool Monitor in the following ways:
■
■
Using JMX
The drivers register a single Statement Pool Monitor for each statement pool with the default MBean server of the JVM. 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
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 ddBMeans = new ObjectName("com.ddtek.jdbc:*");
      Set<ObjectName> mbeans = server.queryNames(ddBMeans, 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);
   }
}
NOTE: Because JMX is not supported by J2SE 1.4, applications running in these JVMs must use the DataDirect-specific method for accessing the Statement Pool Monitor (see “Using DataDirect-Specific Methods”).
Using DataDirect-Specific Methods
The ExtConnection.getStatementPoolMonitor() method returns an ExtStatementPoolMonitor object for the statement pool associated with the connection. This method is provided by the ExtConnection interface in the com.ddtek.jdbc.extensions package. If the connection does not have a statement pool, the method returns null.
Once you have an ExtStatementPoolMonitor object, you can use the poolEntries() method of the ExtStatementPoolMonitorMBean interface implemented by the ExtStatementPoolMonitor to return a list of statements in the statement pool as an array.
Using the poolEntries() Method
Using the poolEntries() method, your application can return all statements in the pool or filter the list based on the following criteria:
■
■
■
Table 10-1 lists the parameters and the valid values supported by the poolEntries() method.
Returns all statements regardless of statement type
Returns only statements with forward-only result sets
Returns only statements with scroll insensitive result sets
Returns only statements with scroll sensitive result sets
Returns statements regardless of result set type
Returns only statements with a read-only result set concurrency
Returns only statements with an updateable result set concurrency
Returns statements regardless of result set concurrency type
The result of the poolEntries() method is an array that contains a String entry for each statement in the statement pool using the format:
SQL_TEXT=[SQL_text];STATEMENT_TYPE=TYPE_PREPARED_STATEMENT|TYPE_CALLABLE_STATEMENT;
RESULTSET_TYPE=TYPE_FORWARD_ONLY|TYPE_SCROLL_INSENSITIVE|TYPE_SCROLL_SENSITIVE;
RESULTSET_CONCURRENCY=CONCUR_READ_ONLY|CONCUR_UPDATABLE;
AUTOGENERATEDKEYSREQUESTED=true|false;REQUESTEDKEYCOLUMNS=comma-separated_list
where SQL_text is the SQL text of the statement and comma-separated_list is a list of column names that will be returned as generated keys.
For example:
SQL_TEXT=[INSERT INTO emp(id, name) VALUES(99, ?)];
STATEMENT_TYPE=Prepared Statement;RESULTSET_TYPE=Forward Only;
RESULTSET_CONCURRENCY=Read Only;AUTOGENERATEDKEYSREQUESTED=false;
REQUESTEDKEYCOLUMNS=id,name
Example: Generating a List of Statements in the Statement Pool
The following code shows how to return an ExtStatementPoolMonitor object using a connection and generate a list of statements in the statement pool associated with the connection.
private void run(String[] args) {
 
   Connection con = null;
   PreparedStatement prepStmt = null;
   String sql = null;
 
   try {
 
      // Create the connection and enable statement pooling
      Class.forName("com.ddtek.jdbc.sqlserver.SQLServerDriver");
      con = DriverManager.getConnection(
         "jdbc:datadirect:sqlserver://SMITH:1440;" +
         "maxPooledStatements=10", "test", "test");
 
      // Prepare a couple of statements
      sql = "INSERT INTO employees (id, name) VALUES(?, ?)";
      prepStmt = con.prepareStatement(sql);
      prepStmt.close();
 
      sql = "SELECT name FROM employees WHERE id = ?";
      prepStmt = con.prepareStatement(sql);
      prepStmt.close();
 
      ExtStatementPoolMonitor monitor =
         ((ExtConnection) con).getStatementPoolMonitor();
 
      System.out.println("Statement Pool - " + monitor.getName());
      System.out.println("Max Size:    " + monitor.getMaxSize());
      System.out.println("Current Size: " + monitor.getCurrentSize());
      System.out.println("Hit Count:    " + monitor.getHitCount());
      System.out.println("Miss Count:   " + monitor.getMissCount());
      System.out.println("Statements:");
 
      ArrayList statements = monitor.poolEntries(-1, -1, -1);
      Iterator itr = statements.iterator();
      while (itr.hasNext()) {
 
         String entry = (String)itr.next();
         System.out.println(entry);
      }
}   
catch (Throwable except) {
 
   System.out.println("ERROR: " + except);
   }
   finally {
 
      if (con != null) {
 
         try {
            con.close();
         }
         catch (SQLException except) {}
         }
      }
   }
In the previous code example, the PoolEntries() method returns all statements in the statement pool regardless of statement type, result set cursor type, and concurrency type by specifying the value -1 for each parameter as shown in the following code:
ArrayList statements = monitor.poolEntries(-1, -1, -1);
We could have easily filtered the list of statements to return only prepared statements that have a forward-only result set with a concurrency type of updateable using the following code:
ArrayList statements = monitor.poolEntries(
   ExtStatementPoolMonitor.TYPE_PREPARED_STATEMENT,
   ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);