P6Spyjava
在優化Hibernate性能的時候,很重要的一點就是要看到Hibernate底層執行的SQLmysql
雖然經過打印日誌配合Hibernate的show_sql屬性可以拼湊出Hibernate底層執行的SQL,可是很不方便,也不直觀sql
下載地址:http://sourceforge.net/projects/p6spy/files/p6spy/apache
使用起來很是簡單(項目只使用Hibernate,沒有使用Spring):oracle
1,將p6spy.jar加入項目工程classpath中app
2,將p6spy配置文件spy.properties加入項目工程/src目錄下工具
3,修改Hibernate配置文件hibernate.cfg.xml: 性能
<!-- <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> --> <property name="connection.driver_class"> com.p6spy.engine.spy.P6SpyDriver </property>
4,修改p6spy配置文件:優化
驅動修改成Hibernate所使用的驅動this
# 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 realdriver=com.mysql.jdbc.Driver
deregisterdrivers必定要修改成true,緣由註釋已經說的很清楚了
#the DriverManager class sequentially tries every driver that is #it's possible to registered to find the right driver.In some instances, #load up the realdriver before the p6spy driver, in which case #your connections will not get wrapped as the realdriver will "steal" #the connection before p6spy sees it. Set the following property to "true" #to cause p6spy to explicitily deregister the realdrivers deregisterdrivers=true
修改日誌記錄方式,能夠選擇控log4j控制日誌輸出、控制檯輸出日誌、文件記錄日誌:
#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=./log/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.appender.CHAINSAW_CLIENT=org.apache.log4j.net.SocketAppender #log4j.appender.CHAINSAW_CLIENT.RemoteHost=localhost #log4j.appender.CHAINSAW_CLIENT.Port=4445 #log4j.appender.CHAINSAW_CLIENT.LocationInfo=true log4j.logger.p6spy=INFO,STDOUT
spy.log:
1399255288210|4|0|statement|select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from TEST_USER user0_ where user0_.id>2|select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from TEST_USER user0_ where user0_.id>2 1399255288217|-1||resultset|select user0_.id as id0_, user0_.name as name0_, user0_.age as age0_ from TEST_USER user0_ where user0_.id>2|age0_ = 30, id0_ = 3, name0_ = ll
SQL Proflier
P6Spy的日誌很是繁瑣,很難進行統計
SQL Profiler是一款基於P6Spy的圖形化監控工具,不但可以監控SQL執行,統計SQL的執行結果,還能根據SQL的執行效能提供優化建議
下載地址:http://sourceforge.net/projects/sqlprofiler/files/sqlprofiler/
要想在原有的P6Spy的基礎上運行SQL Profiler,須要對P6Spy的配置文件進行一些修改:
#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 #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.appender.SQLPROFILER_CLIENT=org.apache.log4j.net.SocketAppender log4j.appender.SQLPROFILER_CLIENT.RemoteHost=localhost log4j.appender.SQLPROFILER_CLIENT.Port=4445 log4j.appender.SQLPROFILER_CLIENT.LocationInfo=true #log4j.logger.p6spy=INFO,STDOUT log4j.logger.p6spy=DEBUG, SQLPROFILER_CLIENT
而後將sqlprofiler.jar放至e盤下,打開cmd運行java -Xms200m -Xmx512m -jar sqlprofiler.jar便可
更加詳細的介紹,參見:http://www.ibm.com/developerworks/cn/java/j-lo-p6spy/