Javamelody Monitoring SQL

Source: Internet
Author: User

Objective

  The basic configuration of Javamelody is described earlier, and here is a simple introduction to using Javamelody to monitor JDBC and SQL.

  Hand code is not easy, reproduced please specify:xingoo

  

Search for a lot of information on the Internet, only two posts in the open source community a little help, but for the monitoring of SQL still have a lot of problems, a lot of netizens met with me the same problem, monitoring page open can be monitoring not data,SQL column anyway is 0, or Nan.

The problem is that the data source part is not configured correctly , so here are two ways to configure it.

First, directly configure the data source to add additional JDBC drivers

According to Userguide's documentation, you can use Jndi to configure the way the data source is configured, such as if Hibernate is used, in Hinernate.cfg.xml

<property name="Hibernate.connection.driver_class">net.bull.javamelody.JdbcDriver</property> <property name="Hibernate.connection.driver">com.mysql.jdbc.Driver</property> <property name="Hibernate.connection.url">jdbc:mysql://localhost:3306/myschema</property><property name="Hibernate.connection.username">myuser</property> <property name="Hibernate.connection.password">mypassword</property>

Note this place, probably the general hibernate.cfg.xml parameter is not like the above configuration, it does not matter.

As long as the original Connection.driver is the real driver, add a parameter above connection.driver_class is javamelody the JDBC driver. Refer to the Hibernate data source file of Oracle below me

<?xml version="1.0"encoding="GBK"?> <!--Specify DTD information for hibernate profiles--<! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-CONFIGURATION-3.0.DTD"> <!--hibernate-configuration is the root element of the connection configuration file--
<!--look here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --><!--Specify the driver to connect to the database note the following sentence!!!! That's the key thing.-<property name="Connection.driver_class">net.bull.javamelody.JdbcDriver</property>
<property name="Connection.driver">oracle.jdbc.driver.OracleDriver</property> <!--Specifies the database name of the Url,hibernate connection connecting to the database---<property name="Connection.url">jdbc:oracle:thin: @localhost:1521:orcl</property> <property name="Connection.useunicode">true</property> <property name="connection.characterencoding">gbk</property> <!--Specify the user name to connect to the database--<property name="Connection.username">test</property> <!--Specify the password to connect to the database-<property name="Connection.password">test</property> <!--c3p0 Connection pool Settings--<property name="Hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--Specify the maximum number of connections in the connection pool-<property name="hibernate.c3p0.max_size"> -</property> <!--Specify the minimum number of connections in the connection pool--<property name="hibernate.c3p0.min_size">1</property> <!--specified connection pool timeout time-to-<property name="Hibernate.c3p0.timeout"> -</property> <!--Specify the maximum number of cache statement objects in the connection pool--<property name="hibernate.c3p0.max_statements">0</property> <property name="Hibernate.c3p0.idle_test_period"> -</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.validate">true</property> <property name="Hibernate.c3p0.preferredTestQuery">SelectSysdate fromDual </property> <property Name="Hibernate.c3p0.idleConnectionTestPeriod"> -</property> <property name="Hibernate.c3p0.maxIdleTime">1800</property> <property name="Hibernate.c3p0.testConnectionOnCheckout">true</property> <!--Specify database dialect--<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!--automatically create databases as needed-<property name="Hbm2ddl.auto">update</property> <!--shows the SQL---<property name= generated by hibernate persistence"Show_sql">true</property> <!--format the SQL script and then output--<property name="Hibernate.format_sql">true</property> <!--list all mapping files--<mapping resource="Person.hbm.xml"/> </session-factory>

Refer to the above configuration, you can do it. Open the Monitoring page and you'll see the SQL-related parameters.

Another way is to use spring, and if you use spring, you don't need to set the driver class for extra.

The prerequisite is that the spring configuration file that is loaded must be specified when the Web. XML is loaded. There is a need to implement a listener in Web. XML, which will enable the application to load the specified spring file when it reads XML.

<listener> <listener-class></listener-class

Then we set the spring file to start by setting parameters

        <context-param>          <param-name>contextConfigLocation</param-name>          <param-value>                 classpath:net/bull/javamelody/monitoring-spring.xml                classpath:context/  Services.xml                classpath:context/data-access-layer.xml                /web-inf/  Applicationcontext.xml          </param-value>        </context-param>  

Note the position of Monitoring-spring.xml and applicaitoncontext.xml, I have not been successful in using this method many times, it seems that the order of this position is reversed. Whether this is the reason, it remains to be verified (tomorrow test, no environment now).

For the entire Web. XML configuration file, refer to the following:

<?xml version="1.0"encoding="GBK"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://java.sun.com/xml/ns/javaee"xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"Id="webapp_id"version="3.0"> <!--Specify spring configuration files--><context-param> <param-name> contextconfiglocation</param-name> <param-value>classpath:net/bull/javamelody/monitoring-Spring.xml
Classpath:bean.xml
</param-value> </context-param> <filter> <filter-name>struts</filter-name> < filter-class>org.apache.struts2.dispatcher.filterdispatcher</filter-class> <init-param> <param-name>struts.action.extension</param-name> <param-value>action </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts</ Filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name> Monitoring</filter-name> <filter-class>net.bull.javamelody.MonitoringFilter</filter-class> <init-param> <param-name>log</param-name> <param-value>true</param-value> </ init-param> </filter> <filter-mapping> <filter-name>monitoring</filter-name> < url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> Net.bull.javamelody.sessionlistener</listener-class> </listener>
<!--The following to specify the listener, will allow the Web app to load the spring configuration file instead of reading--><listener> every time you wait for it to be used <listener-class> Org.springframework.web.context.ContextLoaderListener </listener-class> </listener> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app >

Also, depending on the official documentation, if your app conflicts with Monitoring-spring.xml or AOP, use the monitoring-spring-datasource.xml file instead Monitoring-spring.xml , this file contains only a datasource sending process and An example of Springdatasourcefactorybean .

Hand code is not easy, reproduced please specify: Xingoo

Javamelody Monitoring SQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.