利用p6spy記錄和分析jdbc sql日志
p6spy 可以輸出日志到文件中、控制臺、或者傳遞給 Log4j,而且還能配搭 SQL Profiler 或 IronTrackSQL 圖形化監控 SQL 語句,監測到哪些語句的執行是耗時的,逐個優化。關于與 SQL Profiler 或 IronTrackSQL 的配合使用可參數文件的鏈接。
p6spy在sourceforge上下載:http://sourceforge.net/projects/p6spy/?source=dlp
p6spy的配置:
第一:p6spy.jar放入應用的classpath下
第二:修改連接池或者連接配置的jdbc的驅動為p6spy所提供的驅動,com.p6spy.engine.spy.P6SpyDriver
在單獨的Hibernate的應用中,數據庫驅動配置在hibernate.cfg.xml里面,所以我需要配置文件中的connection.driver_class屬性從oracle.jdbc.driver.OracleDriver改為com.p6spy.engine.spy.P6SpyDriver其他的用戶名密碼等等配置信息全部不用修改.在web程序中,配置的連接池部分,也只需要修改jdbc-driver的配置即可。Hibernate.cfg.xml:
<session-factory> <!-- 在這里改成p6spy提供的數據源 --> <property name="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">scott</property> <property name="connection.password">tiger</property> <property name="connection.pool_size">1</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">false</property> <property name="hibernate.jdbc.batch_size">0</property> </session-factory>第三: 修改 spy.properties 并將其放到classpath下:
(文件內容太多了,僅附上幾處重要的)
################################################################# # MODULES # # # # Modules provide the P6Spy functionality. If a module, such # # as module_log is commented out, that functionality will not # # be available. If it is not commented out (if it is active), # # the functionality will be active. # # # # Values set in Modules cannot be reloaded using the # # reloadproperties variable. Once they are loaded, they remain # # in memory until the application is restarted. # # # ################################################################# #第一:module.log的屬性必須配置,如果不配置,P6SPY將不起任何作用,典型配置: module.log=com.p6spy.engine.logging.P6LogFactory #module.outage=com.p6spy.engine.outage.P6OutageFactory ################################################################# # REALDRIVER(s) # # # # In your application server configuration file you replace the # # "real driver" name with com.p6spy.engine.P6SpyDriver. This is # # where you put the name of your real driver P6Spy can find and # # register your real driver to do the database work. # # # # If your application uses several drivers specify them in # # realdriver2, realdriver3. See the documentation for more # # details. # # # # Values set in REALDRIVER(s) cannot be reloaded using the # # reloadproperties variable. Once they are loaded, they remain # # in memory until the application is restarted. # # # ################################################################# #第二:數據庫驅動配置,你懂的,不多說了 # oracle driver # realdriver=oracle.jdbc.driver.OracleDriver # mysql Connector/J driver # realdriver=com.mysql.jdbc.Driver # informix driver # realdriver=com.informix.jdbc.IfxDriver # ibm db2 driver # realdriver=COM.ibm.db2.jdbc.net.DB2Driver # the mysql open source driver realdriver=org.gjt.mm.mysql.Driver #specifies another driver to use realdriver2= #specifies a third driver to use realdriver3= #第三:appender配置,一般分為三種 #specifies the appender to use for logging #appender=com.p6spy.engine.logging.appender.Log4jLogger #控制臺 #appender=com.p6spy.engine.logging.appender.StdoutLogger appender=com.p6spy.engine.logging.appender.FileLogger # name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log) (used for file logger only) #日志文件存放路徑及文件名 logfile = spy.log # append to the p6spy log file. if this is set to false the # log file is truncated every time. (file logger only) append=true #The following are for log4j logging only log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n log4j.logger.p6spy=INFO,STDOUT
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!