FYI - I wrote a simple class (JMXResultSet) that converts a JDBC result set into a javax.management.openmbean.TabularData object. This allows any ResultSet to be returned to a JMX client. It's simple in that it just ignores columns that can't be transmitted over JMX easily, e.g. CLOB, binary types etc.

This makes it possible to write a JMX MBean that returns a query or really easy to expose DatabaseMetaData through JMX, e.g.

Arbitrary query as a JMX bean

    public TabularData runQuery(String sql) throws Exception {
        try {
            Connection conn = ds.getConnection();

            Statement s = conn.createStatement();

            ResultSet rs = s.executeQuery(sql);

TabularData data = JMXResultSet.getTabularData("QueryResult", sql, rs);

            s.close();
            conn.close();
            return data;
        } catch (SQLException e) {

            throw new SQLException(e.getSQLState(), e.getMessage());
        }
    }

DatabaseMetaData.getTables() as a MBean operation:

public TabularData getTables(String schema, String table) throws Exception {
        try {
            Connection conn = ds.getConnection();

            DatabaseMetaData dmd = conn.getMetaData();

            TabularData data = JMXResultSet.getTabularData("TABLES",
                    "DatabaseMetaData.getTables()",
                    dmd.getTables(null, schema, table, null));

            conn.close();
            return data;
        } catch (SQLException e) {

            throw new SQLException(e.getSQLState(), e.getMessage());
        }
    }


One can then use the features of jconsole to navigate through the data.

It requires JDK 1.5 but I'll work on a good place for it in the tree (might be useful for application MBeans) if it seems useful.

Dan.

Reply via email to