P6Spy 监控JDBC详细配置说明
author:ddwcyl
P6SPY 是一个监控JDBC执行语句的开源产品,利用P6SPY很容易的就能监控到JDBC中执行的SQL语句,便于系统调试和性能调优。
P6SPY 实现原理是对JDBC的关键类进行了一次包装,让应用系统调用自己的类;自己的类截获到SQL语句后再调用真实的JDBC驱动进行执行SQL,这样,在自己的类里面就可以监控到所有的SQL语句。
P6SPY 使用非常简单,只需要简单的几步即完成配置:
一. 将p6spy.jar包放到应用的classpath所在的路径中;
二. 修改连接池或者连接配置的jdbc的驱动为p6spy所提供的保证后的驱动,com.p6spy.engine.spy.P6SpyDriver
三. 修改spy.properties并将其放到类搜索目录.
下面,我们以Hibernate 的配置文件为例进行配置文件的说明。
第一步:将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典型配置如下(注意其中的红色粗体部分的配置):
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.p6spy.engine.spy.P6SpyDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:hero</property>
<property name="connection.username">hr</property>
<property name="connection.password">hr</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">false</property>
<property name="hibernate.jdbc.batch_size">0</property>
</session-factory>
</hibernate-configuration>
第三步.修改spy.properties并将其放到类搜索目录.
说明:
spy.properties中需要注意的地方较多.
(1) module.log的属性必须配置,如果不配置,P6SPY将不起任何作用,典型配置为:module.log=com.p6spy.engine.logging.P6LogFactory
(2) realdriver表示真实的驱动,上面配置的com.p6spy.engine.spy.P6SpyDriver会最终调用真是的驱动程序执行SQL
Oracle数据库的配置为:
realdriver=oracle.jdbc.driver.OracleDriver
(3) appender配置,一般分为三种
#appender=com.p6spy.engine.logging.appender.Log4jLogger
# appender=com.p6spy.engine.logging.appender.StdoutLogger
#appender=com.p6spy.engine.logging.appender.FileLogger
其中,appender=com.p6spy.engine.logging.appender.StdoutLogger表示将输出的SQL或者日志信息输出到Console窗口。
(4) log4j.logger.p6spy一般需要将log4j的相关参数从默认的
log4j.logger.p6spy=info,STDOUT
修改为
log4j.logger.p6spy=warn,STDOUT
以减少p6spy信息的输出
(5) 可以将dateformat=修改为dateformat=hh:mm:ss,SSS有利用时间信息的查看。
(6) 尽量根据P6Spy的标准的示例的配置文件进行修改,不用随便删除配置信息。
典型spy. 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=org.gjt.mm.mysql.Driver
#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=hh:mm:ss,SSS
#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.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=warn,STDOUT
#################################################################
# 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
相关推荐
P6Spy是一个强大的开源工具,专门用于监控JDBC连接,特别是在SpringBoot环境下,它可以提供对Oracle数据库查询执行时长的详细分析。P6Spy的工作原理是通过代理模式,拦截SQL语句并记录其执行情况,这有助于开发者...
4. **配置数据库连接**:在应用程序的数据库连接URL中加入P6Spy的特定后缀,例如`jdbc:p6spy:mysql://localhost:3306/mydatabase`。 5. **启动应用**:重新启动应用,P6Spy即开始生效。 ### 四、日志分析与性能...
本文将详细介绍如何在WebLogic中配置P6Spy,并利用SQLProfiler进行SQL监控。 ### 1. P6Spy的安装与配置 首先,需要下载P6Spy的最新版本,并将其添加到项目的类路径中。P6Spy的核心组件是`spy.jar`,它提供了一个...
P6Spy通过拦截并记录JDBC调用来获取数据库查询、更新和其他操作的详细信息,这对于性能优化、调试和审计非常有用。它的主要功能包括: 1. **SQL日志记录**:P6Spy能够捕获并记录应用程序执行的所有SQL语句,包括...
通常在`jdbc.properties`或`application.properties`等配置文件中,设置如下: ```properties driver=com.p6spy.engine.spy.P6SpyDriver ``` 4. **配置P6Spy**:创建一个名为`spy.properties`的配置文件,用于...
5. **配置集成**:P6Spy的配置通常在`spy.properties`文件中进行,包括日志格式、监控规则等。我们需要为自定义监控器创建类似的配置机制,以便用户可以根据需要调整监控行为。 6. **异常处理**:当SQL执行失败时,...
`spy.properties`是P6Spy的配置文件,用于设置各种监控参数。 2. **配置spy.properties**:打开`spy.properties`文件,你需要根据你的数据库连接信息进行相应的配置,如数据库驱动名、JDBC URL、用户名和密码等。...
P6Spy的核心组件是一个名为`spy.properties`的配置文件,其中可以设置日志文件的位置、要监视的数据库驱动等参数。 以下是在Tomcat应用服务器上集成P6Spy的步骤: 1. 下载P6Spy的最新版本,通常可以从其官方网站...
【P6Spy简介】 P6Spy 是一个开源的 Java 监控工具,专门用于 SQL ...综上所述,P6Spy 是一个强大的 Java 数据库监控工具,通过其灵活的配置和丰富的日志信息,开发者可以深入理解数据库操作,提高应用的性能和稳定性。
在介绍如何在MyEclipse中配置P6Spy以及导入其源码之前,首先需要了解P6Spy是一款什么工具。...如果在配置过程中遇到问题,建议参考P6Spy的官方文档,其中会详细介绍所有配置选项和可能遇到的问题解决方案。
2. **配置代理驱动**:在`jdbc.properties`或类似配置文件中,设置P6Spy为数据库连接的代理驱动。例如: ```properties driver=com.p6spy.engine.spy.P6SpyDriver ``` 注意,这里`...
然后,根据文档配置相应的环境,如修改数据库连接URL以启用P6Spy的代理驱动,设置SqlProfiler的监控参数。对于P6Spy,我们可以在spys.properties中调整日志级别和输出格式,以满足不同需求。对于SqlProfiler,我们...
P6Spy 用 Log4J 来记录 JDBC 调用的日记信息。 自从 2003 年 11 月 30 日 P6Spy 版本 1.3 发布后,已经被下载超过 19,000 次了。P6Spy 支持 WebSphere、WebLogic、JBoss、Resin 和 Tomcat 等绝大多数的应用服务器。
设置`jdbc.url`属性,使其包含P6Spy的特定参数,例如: ``` jdbc.url=jdbc:p6spy:mysql://localhost:3306/mydb?spylib=path/to/p6spy.jar ``` 3. **配置日志**:P6Spy使用自己的日志框架,所以我们需要在应用...
在原版p6spy中,用户需要配置`spy.properties`文件,设置日志格式、输出路径等参数,而这个精简版可能已经预设了更为友好的默认配置,降低了使用门槛。 其次,了解p6spy的源码有助于我们深入理解其工作原理。p6spy...
**P6Spy简介** ...总结,P6Spy通过其丰富的配置选项,如文件大小设置,提供了强大的数据库监控能力。正确配置并利用这些选项,可以帮助我们更好地理解和优化应用与数据库的交互,提升系统的整体性能。
配置P6Spy需要在应用的JDBC连接字符串中插入P6Spy的驱动,并在配置文件中设置日志格式和级别。 以下是配置P6Spy的基本步骤: 1. 下载并添加P6Spy的jar文件到项目的类路径中。 2. 修改JDBC连接字符串,使其指向P6Spy...
- **配置 P6Spy**:在项目资源目录下创建 spy.properties 文件,设置日志输出路径、格式和其他配置选项。 - **启动监控**:启动应用程序后,P6Spy 将开始记录并输出 SQL 信息。 5. **日志配置与定制** - **日志...
P6Spy 是一个开源的数据库监视工具,它通过拦截 JDBC 调用来获取数据库操作的详细信息。这个工具在不修改应用程序代码的情况下,可以帮助开发者和DBA监控数据库访问行为,包括SQL语句的执行时间、性能分析以及日志...
相比其他数据库监控工具,如Log4j的JDBC Appender,P6Spy的优势在于无需修改应用程序代码,只需配置即可生效,而且提供更丰富的信息,如SQL参数、执行计划等。 9. **注意事项** - 配置P6Spy时,务必确保不影响...