如何在Oracle的JDBC诊断中正确获取名称?

时间:2016-10-11 15:35:53

标签: java oracle jdbc

我在" JDBC中的可诊断性"中遵循了Oracle的示例代码。在:https://docs.oracle.com/cd/B28359_01/java.111/b31224/diagnose.htm

我的代码如下所示:

  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

输出看起来像这样: loader = org.apache.catalina.loader.WebappClassLoader@929887595

错误消息如下所示:

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)     在com.mes.tools.DB_Logger。(DB_Logger.java:33)

输出行导致错误,所以如何正确获取名称?

1 个答案:

答案 0 :(得分:0)

试试这个,而不是:

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

Oracle在12c中更新了他们的示例代码。另见:https://docs.oracle.com/database/121/JJDBC/diagnose.htm#JJDBC28887