前言
前面講過了Javamelody的基本配置,這裡簡單的介紹下,如何使用Javamelody來監控JDBC以及SQL。
手碼不易,轉載請注明:xingoo
在網上搜尋很多資料,僅有開源社群上的兩篇文章有點幫助,但對于監控SQL還是有很多問題,有不少的網友遇到了跟我同樣的問題,監控頁面打開可就是監控不到資料,SQL一欄無論如何都是0,要不就是NaN。
這個問題其實還是因為資料源的部分沒有配置正确,這裡介紹兩種配置的方式。
第一種,直接配置資料源,添加額外的jdbc驅動
按照UserGuide的文檔來說,可以使用Jndi配置資料源的方式,比如如果使用Hibernate,那麼在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>
注意這個地方,可能一般的hibernate.cfg.xml參數并不是像上面的配置,不要緊。
隻要保證原有的connection.driver是真是的驅動,上面添加一個參數connection.driver_class是javamelody的那個jdbc驅動即可。即參考我下面诶之oracle的hibernate資料源檔案
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置檔案的DTD資訊 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是連接配接配置檔案的根元素 -->
<hibernate-configuration>
<session-factory>
<!-- 看這裡!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-->
<!-- 指定連接配接資料庫所用的驅動 注意下面這句哦!!!!就是這句話起關鍵性的作用-->
<property name="connection.driver_class">net.bull.javamelody.JdbcDriver</property>
<property name="connection.driver">oracle.jdbc.driver.OracleDriver</property>
<!-- 指定連接配接資料庫的url,hibernate連接配接的資料庫名 -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gbk</property>
<!-- 指定連接配接資料庫的使用者名 -->
<property name="connection.username">test</property>
<!-- 指定連接配接資料庫的密碼 -->
<property name="connection.password">test</property>
<!-- C3P0連接配接池設定-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- 指定連接配接池裡最大連接配接數 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定連接配接池裡最小連接配接數 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定連接配接池裡連接配接的逾時時長 -->
<property name="hibernate.c3p0.timeout">120</property>
<!-- 指定連接配接池裡最大緩存多少個Statement對象 -->
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">60</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.preferredTestQuery ">select sysdate from dual </property>
<property name="hibernate.c3p0.idleConnectionTestPeriod ">120</property>
<property name="hibernate.c3p0.maxIdleTime">1800</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<!-- 指定資料庫方言 -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- 根據需要自動建立資料庫 -->
<property name="hbm2ddl.auto">update</property>
<!-- 顯示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL腳本進行格式化後再輸出-->
<property name="hibernate.format_sql">true</property>
<!-- 羅列所有的映射檔案-->
<mapping resource="Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>
參考上面這樣的配置,就可以了。打開監控頁面,就可以看到SQL相關的參數了。
另一種呢,就是使用spring,如果使用spring,是不需要額外設定驅動類的。
前提是,必須在加載web.xml的時候指定加載的spring配置檔案。需要在web.xml中實作一個監聽,這個監聽回使web應用在讀取web.xml時,加載指定的spring檔案。
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
然後我們通過設定參數,設定啟動的spring檔案
<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>
注意monitoring-spring.xml與applicaitonContext.xml的位置,我好多次使用這種方式都沒有成功,貌似就是這個位置的順序颠倒了。是否是這個原因,還有待驗證(明天測試,現在沒有環境)。
整個web.xml的配置檔案,參考下面:
<?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">
<!-- 指定spring的配置檔案 -->
<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>
<!-- 下面這個用于指定監聽,會使web應用去加載spring的配置檔案,而不是每次等到用的時候讀取-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
另外,根據官方文檔,如果你的應用與monitoring-spring.xml或者AOP之類的有沖突,那麼使用monitoring-spring-datasource.xml檔案替代monitoring-spring.xml就可以了,這個檔案僅僅包含一個datasource的發送程序以及SpringDataSourceFactoryBean的一個例子。
作者:xingoo
出處:http://www.cnblogs.com/xing901022
本文版權歸作者和部落格園共有。歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接!