Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Custom Debugging with WebLogic JMX

Trace your JDBC calls without modifying your existing code

Maintaining complicated legacy applications is a challenge, which is often made worse by lack of documentation, nonintuitive design, and coding practices. Unfortunately almost all software developers will find themselves with such an assignment at some point in their careers.

In the case of any application that utilizes a database, it is very useful to trace SQL statements generated by the application. Such a trace would help with profiling performance bottlenecks, debugging errors, and in facilitating the developer's understanding of the business processes associated with the application.

In the case of legacy applications we would want to do such tracing without changing any code or application configuration. I was able to use WebLogic's JMX API to quickly put together a little code to trace the JDBC calls of a very large and complicated legacy application without impacting the code and configuration of the application. In addition, this small project helped facilitate my understanding of JMX and how WebLogic uses JMX behind the scenes. In this article I will go over the details of using WebLogic JMX to trace SQL statements.

What is JMX?
JMX stands for Java Management Extensions. MBeans, or managed beans, are resources that can be managed through the JMX API. Most application servers use JMX to provide administration consoles and to manage resources. In addition, application developers can use JMX to provide administration and auditing capabilities in their custom applications.

What Advantages Does WebLogic's JMX Implementation Provide for Developers and Administrators?
WebLogic Servers use JMX MBeans for configuration and management. A WebLogic Server each has a copy of its own MBeans, which is updated by the administration server. The administration server maintains a master copy of the MBeans for all of the servers that it manages. If the administration server fails to come up, the managed servers can function based on their local copy of the MBeans until the administration server can become available to update the server's local MBeans.

Web Logic provides both an administration console, which does its work using JMX MBeans, as well as an API to allow application developers to configure and explore WebLogic resources. The easiest way to take advantage of WebLogic JMX is to use the WebLogic console to change the configuration of WebLogic resources and to view the metrics available in the console. While the monitoring and configuration capabilities of the WebLogic console are very powerful and will meet the needs of most applications running on WebLogic, the WebLogic JMX API provides an even more powerful instrument for managing applications running on the WebLogic platform. Using the WebLogic JMX API it is possible to configure and extend WebLogic resources and to also receive notifications from WebLogic's subsystems. For example, an application that is configured with n as the value for minimum and maximum number of JDBC connections might want to have a listener that listens to notifications from the WebLogic JMX MBeans and sends e-mail to an administrator in case the usage of the application crosses n-x concurrent JDBC connections, so that the administrator can decide on an increased value for n and reconfigure the JDBC connection pool(s) (where x is an arbitrary number that depends on the comfort level of the administrator). Examples of advanced uses of JMX by application developers include tracing events in the WebLogic subsystems, including EJB events and server start/stop events.

What Options Are Available for Profiling JDBC Statements in WebLogic Applications?
There are several techniques that can be used to create a dynamic trace of JDBC statements in a WebLogic application. Subclassing the Statement,PreparedStatement and CallableStatement classes from the java.sql package to print a trace using a logging system like Log4J or WebLogic logging and then using the subclasses in the application is a viable option, but it is not practical in the case of legacy code. Such traces may be available from tools such as TOAD, but such tools may not be easily available to application developers and may not provide all of the information that is required. AOP techniques are another valid choice for printing JDBC statements. However, at the time of writing AOP is not officially supported in WebLogic by BEA even though articles about WebLogic AOP have appeared on the dev2dev site. At the time of writing, getting AOP to work in WebLogic is not a trivial project. Using WebLogic JMX with WebLogic 6.1 and 8.1 does not require the use of any additional libraries or configuration, since all the required classes are available in weblogic.jar and the code is very simple to implement. In addition WebLogic JMX is a very mature technology and can be implemented without any changes to the core application code or bytecode.

Using the WebLogic JMX API
The WebLogic javadocs are available online at http://e-docs.bea.com/wls/docs81/javadocs/. The API contains several packages with management in their names. These packages are WebLogic's JMX implementation (see Table 1).

Using JMX to Trace JDBC Calls
A simple way to organize the tracing code and provide a UI to view the SQL is to write a JSP, a servlet, and a Java Bean or object. We'll go through the details of the bean/POJO here and leave most of the details of the UI/controller aspect out since most WebLogic developers already understand these very well. Note that you need not modify any deployment descriptors, database connection pools, or data sources in order to get the tracing to work. All changes to the needed application will happen at run time.

Step 1
First we'll create a class called MyTracerBean.java and import the WebLogic JMX packages and classes that we'll need.

import javax.naming.Context;
import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.configuration.JDBCConnectionPoolMBean;
import weblogic.management.runtime.JDBCStatementProfile;
import weblogic.management.runtime.JDBCConnectionPoolRuntimeMBean;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.naming.NamingException;

See Figure 1

These classes are in the weblogic.jar so you won't need to add any JARs or classes to the WebLogic classpath.

Step 2
Next we'll write a method to get the MBeanHome.


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;
}

More Stories By Salma Saad

Salma started her career back in 1993, working on Steve Jobs NeXT platform and moved on to Java when it was at the ‘bleeding edge’. She has worked as a consultant for more than ten years, working primarily on website development projects for a large variety of clients. In addition to her fascination with everything technical she also has a strong interest in developing her leadership and management expertise. She enjoys living in Chicago and being the mother of two rugrats, ...um boys and is currently working for Arc Worldwide/Leo Burnett helping fortune 500 clients build awe-inspiring websites.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Oscar Madrigal 11/23/05 08:17:44 PM EST

Great tip but only log the succesfull statements, how can trace fails statements?

@ThingsExpo Stories
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
What is the best strategy for selecting the right offshore company for your business? In his session at 21st Cloud Expo, Alan Winters, U.S. Head of Business Development at MobiDev, will discuss the things to look for - positive and negative - in evaluating your options. He will also discuss how to maximize productivity with your offshore developers. Before you start your search, clearly understand your business needs and how that impacts software choices.
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
While some developers care passionately about how data centers and clouds are architected, for most, it is only the end result that matters. To the majority of companies, technology exists to solve a business problem, and only delivers value when it is solving that problem. 2017 brings the mainstream adoption of containers for production workloads. In his session at 21st Cloud Expo, Ben McCormack, VP of Operations at Evernote, will discuss how data centers of the future will be managed, how th...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that B2Cloud will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. B2Cloud specializes in IoT devices for preventive and predictive maintenance in any kind of equipment retrieving data like Energy consumption, working time, temperature, humidity, pressure, etc.
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
Elon Musk is among the notable industry figures who worries about the power of AI to destroy rather than help society. Mark Zuckerberg, on the other hand, embraces all that is going on. AI is most powerful when deployed across the vast networks being built for Internets of Things in the manufacturing, transportation and logistics, retail, healthcare, government and other sectors. Is AI transforming IoT for the good or the bad? Do we need to worry about its potential destructive power? Or will we...
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...