`

利用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 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=oracle.jdbc.driver.OracleDriver

#specifies another driver to use
realdriver2=
#specifies a third driver to use
realdriver3=


#the DriverManager class sequentially tries every driver that is
#registered to find the right driver.  In some instances, it's possible to
#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=false

################################################################
# P6LOG SPECIFIC PROPERTIES                                    #
################################################################
# no properties currently available

################################################################
# EXECUTION THRESHOLD PROPERTIES                               #
################################################################
# This feature applies to the standard logging of P6Spy.       #
# While the standard logging logs out every statement          #
# regardless of its execution time, this feature puts a time   #
# condition on that logging.  Only statements that have taken  #
# longer than the time specified (in milliseconds) will be     #
# logged.  This way it is possible to see only statements that #
# have exceeded some high water mark.                          #
# This time is reloadable.                                     #
#
# executionthreshold=integer time (milliseconds)
#
executionthreshold=

################################################################
# P6OUTAGE SPECIFIC PROPERTIES                                 #
################################################################
# Outage Detection
#
# This feature detects long-running statements that may be indicative of
# a database outage problem. If this feature is turned on, it will log any
# statement that surpasses the configurable time boundary during its execution.
# When this feature is enabled, no other statements are logged except the long
# running statements. The interval property is the boundary time set in seconds.
# For example, if this is set to 2, then any statement requiring at least 2
# seconds will be logged. Note that the same statement will continue to be logged
# for as long as it executes. So if the interval is set to 2, and the query takes
# 11 seconds, it will be logged 5 times (at the 2, 4, 6, 8, 10 second intervals).
#
# outagedetection=true|false
# outagedetectioninterval=integer time (seconds)
#
outagedetection=false
outagedetectioninterval=

################################################################
# COMMON PROPERTIES                                            #
################################################################

# filter what is logged
filter=false

# comma separated list of tables to include when filtering
include     =
# comma separated list of tables to exclude when filtering
exclude     =

# sql expression to evaluate if using regex filtering
sqlexpression =


# turn on tracing
autoflush   = true

# sets the date format using Java's SimpleDateFormat routine
dateformat=

#list of categories to explicitly include
includecategories=

#list of categories to exclude: error, info, batch, debug, statement,
#commit, rollback and result are valid values
excludecategories=info,debug,result,batch


#allows you to use a regex engine or your own matching engine to determine
#which statements to log
#
#stringmatcher=com.p6spy.engine.common.GnuRegexMatcher
#stringmatcher=com.p6spy.engine.common.JakartaRegexMatcher
stringmatcher=

# prints a stack trace for every statement logged
stacktrace=false
# if stacktrace=true, specifies the stack trace to print
stacktraceclass=

# determines if property file should be reloaded
reloadproperties=false
# determines how often should be reloaded in seconds
reloadpropertiesinterval=60

#if=true then url must be prefixed with p6spy:
useprefix=false

#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.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


#################################################################
# DataSource replacement                                        #
#                                                               #
# Replace the real DataSource class in your application server  #
# configuration with the name com.p6spy.engine.spy.P6DataSource,#
# then add the JNDI name and class name of the real   #
# DataSource here                 #
#                                                               #
# Values set in this item cannot be reloaded using the          #
# reloadproperties variable.  Once it is loaded, it remains     #
# in memory until the application is restarted.                 #
#                                                               #
#################################################################
#realdatasource=/RealMySqlDS
#realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource

#################################################################
# DataSource properties                                         #
#                                                               #
# If you are using the DataSource support to intercept calls    #
# to a DataSource that requires properties for proper setup,    #
# define those properties here. Use name value pairs, separate  #
# the name and value with a semicolon, and separate the         #
# pairs with commas.                                            #
#                              #
# The example shown here is for mysql                          #
#                                                               #
#################################################################
#realdatasourceproperties=port;3306,serverName;ibmhost,databaseName;mydb


#################################################################
# JNDI DataSource lookup                                        #
#                                                               #
# If you are using the DataSource support outside of an app     #
# server, you will probably need to define the JNDI Context     #
# environment.                                                  #
#                                                               #
# If the P6Spy code will be executing inside an app server then #
# do not use these properties, and the DataSource lookup will   #
# use the naming context defined by the app server.             #
#                                                               #
# The two standard elements of the naming environment are #
# jndicontextfactory and jndicontextproviderurl. If you need    #
# additional elements, use the jndicontextcustom property.      #
# You can define multiple properties in jndicontextcustom,      #
# in name value pairs. Separate the name and value with a       #
# semicolon, and separate the pairs with commas.                #
#                                                               #
# The example shown here is for a standalone program running on #
# a machine that is also running JBoss, so the JDNI context     #
# is configured for JBoss (3.0.4).                              #
#                                                               #
#################################################################
#jndicontextfactory=org.jnp.interfaces.NamingContextFactory
#jndicontextproviderurl=localhost:1099
#jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.nameing:org.jnp.interfaces

#jndicontextfactory=com.ibm.websphere.naming.WsnInitialContextFactory
#jndicontextproviderurl=iiop://localhost:900



SQL Profiler无须安装,直接实用
* 我们先用java -jar sqlprofiler.jar 运行 sql profiler

    我通常的做法是在profiler.jar 的同级目录下建一个start.bat, 只有一行代码:

    java -jar sqlprofiler.jar

    
* 然后启动tomcat,  执行任何SQL就会被profiler截取得。为什么要用这个工具呢?她最大的好处就是把

   insert into   T_UCP_CATEGORY(ID,  CATEGORYNAME,  DESCRIPTION) values(?,?,?)

  转化成真正的SQL:

   insert into   T_UCP_CATEGORY(ID,  CATEGORYNAME,  DESCRIPTION) values('200704022012012','公共表','公共表类型');

一切尽在眼前了吧?

sql Profiler主要有三个按钮:清空,开始,暂停。

   清空: 清空原有所有日志;

   开始: 在你想截取SQL时就点开始启动监听;

   暂停: 暂停监听,日志就不会跳了,你可以拷贝出SQL 去PL/SQL中测试


当然,p6spy 和 sql profiler 能做的不止这些,sql profiler还能根据你的query来帮你生成建立合适的index功能等等。

分享到:
评论

相关推荐

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

    利用P6SPY +SQL Profiler调试、记录、统计web app对数据库的操作, 可以查看到PreparedStatment的执行最终的SQL语句。 有详细的操作说明 下面的这个是集成的完整版。。太大了就不上传了。自己下吧 jahia_v6_...

    Tomcat6.x + P6spy + SqlProfiler集成

    P6spy是一个开源的数据库监控工具,它能够拦截并记录应用程序与数据库之间的SQL交互。通过在数据库连接字符串中插入P6spy驱动,它可以在不修改代码的情况下透明地监控数据库活动。P6spy提供了一个名为`spy....

    weblogic server 11g +p6spy +sql profiler 配置

    SQL Profiler是P6Spy的一个组件,用于对数据库操作进行详细分析,找出潜在的性能瓶颈。 配置WebLogic Server 11g与P6Spy和SQL Profiler的集成,主要涉及以下几个步骤: 1. **安装P6Spy**:首先,你需要下载P6Spy的...

    sqlprofiler+p6spy+配制

    在Java应用中,P6Spy可以捕获和记录所有的SQL请求,而SQL Profiler则可以在服务器端提供更详细的事件追踪。这种组合可以提供从客户端到服务器端的完整查询生命周期的视图,对于诊断复杂的性能问题特别有用。 总的来...

    p6spy_sqlprofiler-0.3-bin.zip

    一旦配置完成,启动应用程序,P6Spy就会开始记录SQL操作。这时,可以运行SQLProfiler,导入P6Spy生成的日志文件,它会自动分析并生成性能报告。报告中可能会包括SQL执行时间的直方图、平均执行时间、最耗时的SQL语句...

    P6spy 和 SqlProfiler 打印JDBC真实SQL

    总结来说,P6Spy和SqlProfiler是Java开发中不可或缺的数据库监控工具。它们能够帮助我们深入了解应用程序与数据库的交互,从而进行有效的性能优化和问题定位。在日常开发和维护过程中,正确使用这两个工具可以显著...

    监控和剖析数据库 P6Spy、SQL Profiler、IronTrack SQL

    本文将详细介绍P6Spy、SQL Profiler和IronTrack SQL这三个工具,帮助你理解和掌握数据库监控与性能分析的方法。 首先,P6Spy是一个开源的Java库,它允许开发者在不修改应用程序代码的情况下,对数据库访问进行透明...

    p6spy_sqlprofiler

    专门用来监控hibernate的sql输出,进行调试应用程序的工具,找了好久才找到,里面有个配置文件,记住realdriver是你的驱动,而在hibernate设置的驱动是这个驱动代理,p6spy的驱动,相当于在hibernate和数据库之间放...

    p6spy和sqlProfiler的配合使用让你的hibernate的show_sql更好用

    p6spy和sqlProfiler的配合使用让你的hibernate的show_sql更好用 p6spy和sqlProfiler的配合使用让你的hibernate的show_sql更好用 p6spy和sqlProfiler的配合使用让你的hibernate的show_sql更好用

    p6spy 在weblogic中的配置 以及使用sqlprofiler监控

    P6Spy是一款开源的数据库监控工具,主要用于记录和分析应用程序对数据库的所有SQL操作。在Java应用环境中,如WebLogic服务器上,P6Spy可以帮助开发者和DBA监控SQL性能,找出可能存在的瓶颈。本文将详细介绍如何在...

    借鉴p6spy,实现自己的SQL执行监控器项目源代码

    P6Spy的工作原理是通过代理JDBC驱动,拦截所有的SQL语句,然后在执行前后添加额外的操作,如日志记录、性能分析等。为了实现自己的SQL执行控制器,我们需要理解以下几个关键知识点: 1. **JDBC驱动代理**:JDBC驱动...

    p6spy-sql监控

    1. **SQL日志记录**:P6Spy可以捕获并记录所有执行的SQL语句,包括原始的SQL、执行时间、返回结果等,这对于追踪数据库操作和性能分析非常有用。 2. **日志格式自定义**:P6Spy允许用户通过配置文件定制日志输出...

    Tomcat6.x + p6spy + IronTrackSQL 监控sql性能

    标题 "Tomcat6.x + p6spy + IronTrackSQL 监控sql性能" 所涉及的知识点主要集中在Java Web服务器的管理和数据库性能监控上。在这个配置中,Tomcat6.x是一个广泛使用的开源Java Servlet容器,它负责运行Java Web应用...

    spy++ 绿色工具

    Spy++ 是一款强大的Windows系统监控工具,主要用于调试和分析应用程序的用户界面(UI)和窗口操作。这个绿色工具被封装在名为"spy++.zip"的压缩包中,无需安装即可直接使用,为开发者提供了一个便捷的环境来检测和...

    64位 Spy++

    64位 Spy++ 是一款专为64位操作系统设计的高效能、轻量级的Windows系统监控工具。这款工具主要用于帮助软件开发者、测试人员以及系统管理员深入理解Windows应用程序的内部运行机制,它提供了窗口、消息、过程以及...

    SPY++源代码

    1. SpyXX.aps:这是Visual Studio的工程文件,记录了项目的配置信息,包括编译设置、依赖库等,为构建SPY++项目提供了基础。 2. SpyXX.clw、SpyX1.clw:这些是ClassWizard生成的类文件,用于管理MFC(Microsoft ...

    spy lite spy++

    **描述:“spy++ 加强版,可获取窗口各类消息,灵活方便操作各类窗口”** **关键词:spylite、spy++** **正文:** `Spy Lite` 和 `Spy++` 是两种强大的Windows应用程序调试和分析工具,尤其在软件开发和测试领域...

    spy++ lite版

    Spy++ Lite版是一款专为那些只安装了Visual Studio Express版,无法使用完整版Spy++的用户设计的轻量化工具。Spy++通常是一个强大的Windows应用程序,用于监控系统中的窗口、消息、线程和进程,它是Microsoft Visual...

    类似SPY++的窗口查看器

    总的来说,创建一个类似SPY++的窗口查看器是一个涉及Windows编程、MFC框架和系统级调试技术的综合项目,对理解Windows操作系统的工作原理和提升编程技能有极大的帮助。通过这个项目,开发者可以深入学习到Windows...

Global site tag (gtag.js) - Google Analytics