private MBeanHome getMBeanHome() {
//URL to the serve whose JDBC activity we are tracing
String url = "t3://localhost:7001";
String username = "mywlconsoleuname";
String password = "mywlconsolepsswd";
//The MBeanHome will allow us to
//retrieve the MBeans related to JDBC statement tracing
MBeanHome home = null;
try { //We'll need the environment so that we can //retrieve the initial context
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password Context ctx = env.getInitialContext();
//Retrieving the MBeanHome interface for the server with //the url t3://localhost:7001
home =(MBeanHome)ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
} catch (NamingException ne) {
System.out.println("Error getting MBeanHome " + ne);
}
return home;
}
对于最简单的情形:管理服务器也驻留了我们要跟踪的JDBC应用程序,上述代码完全可行;但是对于管理服务器独立于托管服务器,并且涉及到几个独立JVM的情形,我们需要获得管理MBeans home而不是本地MBeans home。二者的区别在于,本地home只为单个服务器提供Mbean,而管理home则为管理服务器所管理的所有服务器提供MBean。为了获得管理MBeans home而不是本地MBeans home,可以将上述代码中的LOCAL_JNDI_NAME替换为ADMIN_JNDI_NAME。 步骤3
提供一种打开和关闭JDBC分析的方式是非常有用的,因为分析的开销相当大,所以不需要时应当将其关闭。默认情况下,分析是关闭的,因此必须在跟踪任何JDBC语句前打开它。创建一个如下的方法:
public void configureJDBCAuditing(boolean isOn) {
try {
MBeanHome home = getMBeanHome();
//Retreive the bean to help us configure the Pool
JDBCConnectionPoolMBean mConfigBean =
(JDBCConnectionPoolMBean)home.getConfigurationMBean("MyPool","JDBCConnectionPoolConfig");
mConfigBean.setSqlStmtProfilingEnabled(isOn);
mConfigBean.setSqlStmtParamLoggingEnabled(isOn);
} catch (InvalidAttributeValueException iave) {
System.out.println("Invalid attribute while configuring tracing " + iave);
} catch (InstanceNotFoundException infe) {
System.out.println("Instance not found while configuring tracing " + infe);
}
}
在上述代码中,我们还告知连接池我们希望查看传入SQL语句中的参数。这会增加跟踪的开销,但是它可以为我们提供一些有价值的信息。 步骤4
在配置JDBC池来保存配置文件之后,我们可以对其进行查询。记住,检索到的配置文件数等于打开分析后所执行的SQL语句数,而不等于所有由MyPool ConnectionPool执行的SQL语句数。以下的代码检索配置文件,maxProfiles参数指示应该获取最近的多少个配置文件。创建的方法如下:
/** Pass in -1 to get all profiles */
public JDBCStatementProfile[] getProfiles(int maxProfiles) {
JDBCStatementProfile[] profiles = null;
try {
MBeanHome home = getMBeanHome();
JDBCConnectionPoolRuntimeMBean mbean =
(JDBCConnectionPoolRuntimeMBean)home.getRuntimeMBean("MyPool
","JDBCConnectionPoolRuntime");
int numProfiles = mbean.getStatementProfileCount();
int profilesIndex = 0;
//figure out index to start at and how many we want
if (maxProfiles != -1) {
profilesIndex = numProfiles - maxProfiles;
}else {
maxProfiles = numProfiles;
}
profiles =mbean.getStatementProfiles(profilesIndex,maxProfiles);
} catch (InstanceNotFoundException infe) {
System.out.println("Problem retrieving jdbc profiles " + infe);
}
return profiles;
}
JDBCConnectionPoolRuntimeMBean的getStatementProfiles方法在WebLogic 8.1 API文档中没有提及,尽管它曾在WebLogic 6.1文档中出现。不过这看起来是个错误,因为在WebLogic 8.1中该方法是可用的,并且WebLogic 8.1还修复了方法中的一个bug(CR094729,参见http://e-docs.bea.com/wls/docs81/notes/resolved_sp01.html),这意味着WebLogic 8.1是打算包含该方法的。 步骤5
可以添加一个清空功能,使得重启服务器时可以清空语句缓存:
public void reset() {
MBeanHome home = getMBeanHome();
try {
JDBCConnectionPoolRuntimeMBean mbean =
(JDBCConnectionPoolRuntimeMBean)home.getRuntimeMBean("MyPool
","JDBCConnectionPoolRuntime");
//Remove everything from the cache
mbean.resetStatementProfile();
} catch (InstanceNotFoundException infe) {
System.out.println("Problem while resetting JDBC profiles " + infe);
}
}
步骤6
对配置文件进行迭代,获得要显示的信息(参见清单1)。这些代码可能放在用户界面层,比如放在一个JSP中。 清单1
MyTracerBean myTracer = new MyTracerBean();
//In this case we want the 100 most recently executed statements
JDBCStatementProfile[] profiles = myTracer.getProfiles(100);
//Doing the looping so that the most recent statements information is
//retrieved first
for (int i=profiles.length-1;i>-1;i--) {
//Getting the number of parameters passed into the current //statement
int paramCount = profiles.getParameterCount();
//Format the start and end time for the current statement
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm:ss:SS aaa");
String startTime=simpleDateFormat.format(new Date(profiles.getStartTime()));
String endTime=simpleDateFormat.format(new Date(profiles.getStopTime()));
//Append the parameters together in order to display them
StringBuffer paramsBuffer = new StringBuffer();
if (paramCount < 1) {
paramsBuffer.append("None");
} else {
for (int j=0;j<paramCount;j++) {
paramsBuffer.append(profiles.getParameter(j));
paramsBuffer.append(" ");
}
}
String statementTxt = profiles.getStatementText();
String paramsTxt = paramsBuffer.toString();
String timeTaken = profiles.getTimeTaken()
//Then use statementTxt, paramsTxt, timeTaken, startTime, endTime
// etc to show the statement details in a UI
}
展望WebLogic JMX的前景
在撰写本文时,刚刚发布的WebLogic 9.0支持的是JMX 1.2而不是WebLogic 8.1及以前版本一直支持的JMX 1.0。响应JMX规范的变化,9.0中的WebLogic JMX API有了相当大的变化,清单1中的代码可能会引起不支持的警告。当升级至9.0时,应当用JDBCDataSourceRuntimeMBean替换JDBCConnectionPoolRuntimeMBean。不过,在撰写本文时,绝大多数运行在WebLogic上的遗留应用程序还没有使用WebLogic Server 9.0,而且很可能在相当长的一段时间内不会使用9.0。 原文出处: Custom Debugging with WebLogic JMX http://wldj.sys-con.com/read/138275.htm