日期:2014-05-16  浏览次数:20495 次

利用P6SPY +SQL Profiler记录、统计web app对数据库的操作

弄JDBC,Hibernate,iBATIS时,想显示sql语句,可以设置show_sql为true来达到这个目的,但是参数值全是像PreparedStatement一样,用?来代替的。
用p6spy和SQL Profiler可以达到显示的那些参数原值的目的,可读性很强。然后可以直接把带有参数原值的sql语句拷贝到pl/sql或TOAD中跑一下,非常容易定位sql的错误在哪里。

P6spy是一个JDBC Driver的包装工具,p6spy通过对JDBC Driver的封装以达到对SQL语句的监听和分析,以达到各种目的。

需要下载两个文件:p6spy-install.zip, sqlprofiler-0.3-bin.zip
p6spy:
http://www.p6spy.com
SQL Profile: http://www.jahia.net/jahia/page597.html

p6spy安装:
* 将p6spy.jar放到WEB-INF/lib目录下, 这样应用程序很容易找到;
* 修改你 原有 JDBC Driver为:com.p6spy.engine.spy.P6SpyDriver;

?? 我是这样修改我的应用程中的jdbc.properties文件的:

jdbc1.driver=com.p6spy.engine.spy.P6SpyDriver
jdbc1.url=jdbc:oracle:thin:@192.168.0.166:1521:CSPDEV
jdbc1.username=CSP
#followed password must be encrypted by das tool.
jdbc1.password=170B0B970D2B18DA
jdbc1.dbtype=oracle

jdbc2.driver=com.p6spy.engine.spy.P6SpyDriver
jdbc2.url=jdbc:oracle:thin:@192.168.0.166:1521:CSPDEV
jdbc2.user=CSP
#followed password must be encrypted by das tool.
jdbc2.password=170B0B970D2B18DA
jdbc2.dbtype=oracle
jdbc2.level=2
jdbc2.testsql=select count(*) from dual


* 修改 spy.properties 中的 realdriver 值为 原有 的JDBC Driver,比如我的是:oracle.jdbc.driver.OracleDriver;?? 在这里需要特别注意的是,将spy.properties 放到WEB-INF\classes下不一定成功,我经过多次偿试把spy.properties 分别全放在三地方才成功的:

?????????? WEB-INF\classes????????

?????????? WEB-INF\lib???????????? --即跟p6spy.jar放在一起

?????????? %TOMCAT_HOME%\bin????????--一启动tomcat就知道

我的spy.properties文件如下:

#################################################################
# P6Spy Options File??????????????????????????????????????????? #
# See documentation for detailed instructions?????????????????? #
#################################################################

#################################################################
# 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=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 th