The MySQL Enterprise Plugin for Connector/J lets you use the Query Analyzer to monitor MySQL queries from any application using the Connector/J JDBC driver. As described in Section 3.2, “Query Analyzer User Interface”, the Query Analyzer can help you locate queries that are inefficient or slow. Tuning such queries helps to shorten load times for web pages, and improves overall system responsiveness and scalability.
Information about the queries is sent directly to the MySQL Enterprise Service Manager. Once you install the MySQL Enterprise Plugin for Connector/J, query analysis becomes available for your applications without requiring any modification to the application code.
Table 3.2. Query Analyzer Connector/J Summary
| Feature | C/Java |
|---|---|
| Uses Aggregator | No |
| Uses Proxy | No |
| Supports EXPLAIN | Yes |
MySQL Connector/J version 5.1.12 or later.
JDK-1.5.0 or later.
MySQL Enterprise Service Manager version 2.2 or later.
The MySQL client application user must have
SELECT privileges on the
mysql.inventory table. This table contains
the server UUID; it is required
to report the Query Analyzer data to the MySQL Enterprise Service Manager. Use
the GRANT statement. For example:
mysql> GRANT SELECT on mysql.inventory to 'user'@'localhost' IDENTIFIED BY 'password';
Apache Commons logging in the CLASSPATH of
the application being analyzed. If you are not already using
Commons Logging, modify the application's
CLASSPATH as described in the following
section to point to the JAR file bundled with the MySQL Enterprise Monitor
product.
Place the JAR file
lib/c-java-mysql-enterprise-plugin-
in the application's version.jarCLASSPATH where it is
visible to the version of MySQL Connector/J in use. Ideally, use the
same location as MySQL Connector/J's JAR file, or in a parent
classloader to that JAR file's location.
If the application being analyzed does not have Apache Commons
Logging in the CLASSPATH, install the file
lib/required/commons-logging-1.1.1.jar in the
application's CLASSPATH as well. If no other
component in your application uses Apache Commons Logging, install
it in the same place where the Query Analyzer plugin was installed.
There is static shutdown() method on
com.mysql.etools.jdbc.StatementPerformanceCounters,
which can be used to cleanly shutdown the query analysis plugin when
the application is going to be shutdown.
If the application in question is deployed in a J(2)EE application
server, there is an included ContextListener
distributed with the plugin which will call this method when the
application's context is shutdown (or reloaded). Application Servers
which support @WebListener (such as JEE6 and
above) do not need to do any extra configuration, but users with
older Application Servers need to add the following line to their
application's web.xml file:
<listener>
<listener-class>
com.mysql.etools.jdbc.ContextListener
</listener-class>
</listener>
The Connector/J plugin prior to version 1.0.81 can use a workaround
by placing the plugin in the shared library loader of their
Application Server, rather than in the
WEB-INF/lib directory.
Once the required JAR files are deployed, modify the connection URL for JDBC to use and configure the Query Analyzer plugin. Add the following properties and their corresponding values to the URL that is used by the application you are monitoring.
For more information on Connector/J properties, see Connector/J (JDBC) Reference.
Table 3.3. MySQL Monitor Plugin for Connector/J Options
| Property Name | Property Type | Default |
|---|---|---|
mysqlenterprise.disableSourceLocation | boolean | false |
mysqlenterprise.serviceManagerConnectTimeout | numeric | |
mysqlenterprise.serviceManagerPassword | string | |
mysqlenterprise.serviceManagerResponseTimeout | numeric | |
mysqlenterprise.serviceManagerUrl | string | |
mysqlenterprise.serviceManagerUser | string |
statementInterceptors =
com.mysql.etools.jdbc.StatementPerformanceCounters
Enables the operation of the plugin. Without this configuration option, query information is not provided to MySQL Enterprise Service Manager
mysqlenterprise.disableSourceLocation
Whether to send stack traces with example queries to the service manager.
mysqlenterprise.serviceManagerUrl
| Permitted Values | |||
| Type | string | ||
Specifies the URL to the service manager. You can get this
information from the configuration file created when
MySQL Enterprise Service Manager was installed. You must include the full URL,
including the port number. For example,
http://monitor.example.com:18080.
mysqlenterprise.serviceManagerUser
| Permitted Values | |||
| Type | string | ||
Specifies the agent username to be used when communicating information to MySQL Enterprise Service Manager.
mysqlenterprise.serviceManagerPassword
| Permitted Values | |||
| Type | string | ||
Specifies the agent password to be used when communicating information to MySQL Enterprise Service Manager.
Optional
mysqlenterprise.serviceManagerConnectTimeout
| Version Introduced | 2.3.4 | ||
| Permitted Values | |||
| Type | numeric | ||
The number of seconds to wait for a connection to be made to the service manager before timing out. The default is no timeout. Introduced in Connector/J version 1.0.0.61.
mysqlenterprise.serviceManagerResponseTimeout
| Version Introduced | 2.3.4 | ||
| Permitted Values | |||
| Type | numeric | ||
The number of milliseconds to wait for pending reads or writes to or from the Service Manager to complete before timing out. The default is no timeout. Introduced in Connector/J version 1.0.0.61.
To use SSL to communicate query information to the MySQL Enterprise Service Manager,
configure the following properties. Out of the box, the plugin is
configured to work with the self-signed certificates that ship with
the MySQL Enterprise Monitor. Therefore, it provides confidentiality for the
information that is sent by the plugin if the service manager URL
starts with https://. However, as configured, the
plugin does not prevent man-in-the-middle attacks, nor does it
support client-side certificates for authentication. To enable these
features, change the values of the following parameters:
Table 3.4. MySQL Monitor Plugin for Connector/J SSL Options
| Property Name | Property Type | Default |
|---|---|---|
mysqlenterprise.clientCertificateKeystorePassword | string | |
mysqlenterprise.clientCertificateKeystoreType | string | |
mysqlenterprise.clientCertificateKeystoreUrl | string | |
mysqlenterprise.trustCertificateKeystorePassword | string | |
mysqlenterprise.trustCertificateKeystoreType | numeric | |
mysqlenterprise.trustCertificateKeystoreUrl | string | |
mysqlenterprise.verifySslCerts | boolean | false |
mysqlenterprise.verifySslHostnames | boolean | false |
mysqlenterprise.verifySslHostnames
If set to "true", host names presented in the SSL server certificate are verified following the scheme listed at BrowserCompatHostnameVerifier which is the same scheme as used by Firefox and Curl and specified by RFC 2818.
mysqlenterprise.verifySslCerts
Specifies whether the plugin verifies that the certificate
presented by the server was signed by a CA in the
trustCertificateKeystore.
mysqlenterprise.trustCertificateKeystoreUrl
URL to the trusted root certificate KeyStore (if not specified, use Java defaults).
mysqlenterprise.trustCertificateKeystorePassword
Password for the certificate store.
mysqlenterprise.trustCertificateKeystoreType=[type]
KeyStore type for trusted root certificates (NULL or empty means
use JKS default, standard keystore types
supported by the JVM are JKS and
PKCS12, your environment may have more
available depending on what security products are installed and
available to the JVM.
mysqlenterprise.clientCertificateKeystoreUrl
URL to the client certificate KeyStore (if not specified, use Java defaults)
mysqlenterprise.clientCertificateKeystorePassword=[password]
Password for the client certificate store.
mysqlenterprise.clientCertificateKeystoreType
KeyStore type for client certificates (NULL or empty means use
JKS default, standard keystore types
supported by the JVM are JKS and
PKCS12, your environment may have more
available depending on what security products are installed and
available to the JVM.
For example, to configure a Connector/J to communicate with the MySQL Enterprise Service Manager localhost, on port 18080, using the agent username agent, and password PASSWORD, you would add the following options to your connection string (but define the option on a single line):
statementInterceptors=com.mysql.etools.jdbc.StatementPerformanceCounters &serviceManagerUrl=http://localhost:18080/ &serviceManagerUser=agent &serviceManagerPassword=PASSWORD
Remember that you must also add the application-specific options to
the JDBC URL. For example, the following fragment connects to the
MySQL database test on
localhost, using the user and password of
mysqltest, while also collecting query data and
sending it to the MySQL Enterprise Service Manager on localhost:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=mysqltest" +
"&password=mysqltest" +
"&statementInterceptors=com.mysql.etools.jdbc.StatementPerformanceCounters" +
"&serviceManagerUrl=http://localhost:18080/" +
"&serviceManagerUser=agent" +
"&serviceManagerPassword=PASSWORD"
);
// Do something with the Connection
...
} catch (SQLException ex) {
// handle any errors
}If a DataSource is in use (typically when using Glassfish, Weblogic, or Websphere), these properties must be passed as part of the URL property, they cannot be added to the DataSource configuration itself.
If an alternate logging system has not been selected for Connector/J, it is recommended that Connector/J's log factory is configured to use something other than the standard logger by adding the following property to the URL or DataSource:
logger=Log4JLogger (for applications using
Log4J)
logger=CommonsLogger (for applications using
Apache Commons Logging)
logger=Jdk14Logger (for applications using
Java 1.4 or later logging)
Further configuration of the plugin is done via the Enterprise
Service Manager's user interface, using the Query
Analyzer tab. From here, the capture of query performance
data for a given MySQL instance that the plugin is being with can be
enabled or disabled. Further, the level of detail that is captured;
summary, examples (with source code locations) and
EXPLAIN plans can be configured as well.
The Query Analyzer plugin checks the configuration once each minute (while it is reporting data it has collected), thus configuration changes are not immediate.
After you set up MySQL Enterprise Plugin for Connector/J, you monitor the performance of your Java applications through the Query Analyzer tab, as described in Section 3.2, “Query Analyzer User Interface”.
Be aware of the following while using the MySQL Enterprise Plugin for Connector/J:
When a Java application that is reporting queries terminates, you might lose the information about some queries because the queued query information has not been sent to MySQL Enterprise Service Manager. To work around this issue, add a sleep to your application to ensure the normal reporting schedule submits the outstanding queries. For example, you could add:
Thread.sleep(60000);
to your application, to pause before the application terminates.