How to get name correctly in Oracle's diagnosa

2019-09-05 18:03发布

问题:

I'm following Oracle's sample code in "Diagnosability in JDBC" at : https://docs.oracle.com/cd/B28359_01/java.111/b31224/diagnose.htm

My code looks like this :

  ClassLoader l = oracle.jdbc.OracleDriver.class.getClassLoader();
  String loader = l.getClass().getName() + "@" + l.hashCode();
  System.out.println("loader = "+loader);
  javax.management.ObjectName name=new javax.management.ObjectName("com.oracle.jdbc:type=diagnosability,name="+loader);
  javax.management.MBeanServer mbs=java.lang.management.ManagementFactory.getPlatformMBeanServer();                          // get the MBean server
  System.out.println("LoggingEnabled = "+mbs.getAttribute(name,"LoggingEnabled"));           DB_Logger.java:33               // find out if logging is enabled or not
  mbs.setAttribute(name,new javax.management.Attribute("LoggingEnabled",true));                                              // enable logging
  mbs.setAttribute(name,new javax.management.Attribute("LoggingEnabled",false));                                             // disable logging

The out put looks like this : loader = org.apache.catalina.loader.WebappClassLoader@929887595

The error message looks like this :

javax.management.InstanceNotFoundException: com.oracle.jdbc:type=diagnosability,name=org.apache.catalina.loader.WebappClassLoader@929887595 at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1095) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:643) at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:678) at com.mes.tools.DB_Logger.(DB_Logger.java:33)

The output line is causing the error, so how can I get the name correctly ?

回答1:

Try this, instead:

ClassLoader l = oracle.jdbc.OracleDriver.getClassLoader();
String loader = l.getName() + "@" + l.hashCode();

Oracle updated their sample code in 12c. See also: https://docs.oracle.com/database/121/JJDBC/diagnose.htm#JJDBC28887



标签: java oracle jdbc