集成p6spy到spring中

网上好多帖子都是说,修改原有 JDBC Driver为:com.p6spy.engine.spy.P6SpyDriver

然后修改spy.properties 中的 realdriver值为原有的JDBC Driver

如果在spring开发环境中使用org.springframework.jdbc.datasource.DriverManagerDataSource来模拟dataSource是不会生效的,

Java代码  

  1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  2. <property  name="driverClassName">
  3. <value>com.sybase.jdbc2.jdbc.SybDriver</value>
  4. </property>
  5. <property  name="url">
  6. <value>jdbc:sybase:Tds:192.168.0.2:6100/db</value>
  7. </property>
  8. <property  name="username">
  9. <value>usr</value>
  10. </property>
  11. <property  name="password">
  12. <value>pwd</value>
  13. </property>
  14. </bean>

因为在spring加载时我们无法控制先加载哪个,spring会报错不能使用com.p6spy.engine.spy.P6SpyDriver连接到数据库,

其实p6spy只是想做jdbc的代理,而不是真正想连数据库,而spring却不知道这些,所以应该采用另外一种方式来集成p6spy

Java代码  

  1. <bean id="dataSourceTarget" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  2. <property  name="driverClassName">
  3. <value>com.sybase.jdbc2.jdbc.SybDriver</value>
  4. </property>
  5. <property  name="url">
  6. <value>jdbc:sybase:Tds:192.168.0.2:6100/db</value>
  7. </property>
  8. <property  name="username">
  9. <value>usr</value>
  10. </property>
  11. <property  name="password">
  12. <value>pwd</value>
  13. </property>
  14. </bean>
  15. <bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource"
  16. destroy-method="close">
  17. <constructor-arg>
  18. <ref local="dataSourceTarget"/>
  19. </constructor-arg>
  20. </bean>

有时候会报错cvc-complex-type.3.2.2: 元素 ‘ref‘ 中不允许出现属性 ‘local‘,

只需要把local改成bean就可以了。估计新版本去掉了这个local

这样就可以把p6spy集成到spring中去了。

配置文件spy.properties

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

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

# the hsqldb open source driver
#realdriver=org.hsqldb.jdbcDriver

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

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

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

# sql expression to evaluate if using regex
sqlexpression =

# filter what is logged
filter=false

# turn on tracing
trace = true
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=
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 = c:/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.F=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.F.File=c:/spy.log
log4j.appender.F.layout=org.apache.log4j.PatternLayout
#log4j.appender.F.layout.ConversionPattern=%p %t %c - %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,F

#################################################################
# 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;myhost,databaseName;jbossdb,foo;bar

#################################################################
# 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
时间: 2024-11-11 19:52:26

集成p6spy到spring中的相关文章

SCA与spring集成(在spring中开发SOA)

客户端调用与Tuscany SCA相同 服务端的配置如下: sca.composite <?xml version="1.0" encoding="UTF-8"?> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0" targetNamespace="http://myWebServer"name="myWebService">

spring中集成shiro进行安全管理

shiro是一款轻量级的安全框架,提供认证.授权.加密和会话管理四个基础功能,除此之外也提供了很好的系统集成方案. 下面将它集成到之前的demo中,在之前spring中使用aop配置事务这篇所附代码的基础上进行集成 一.添加jar包引用 修改pom.xml文件,加入: <!-- security --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core&

spring 中集成quartz定时器及quartz中cronExpression配置说明

 spring 中集成quartz: spring文件的配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http:/

spring中集成使用jedis(2)

本文主要就spring注入的连接池使用问题,做简要说明. 使用过JedisPool的同学会发现,通过JedisPool获取资源,在使用完毕后,需要显式的将资源放回连接池中, 如下: JedisPool jedisPool; Jedis jedis = jedisPool.getResource(); //操作 jedisPool.returnResource(jedis); 如果未显示的回收资源,则在连接池中资源使用完毕后,系统会出现阻塞. 因为如果使用连接池,就相当于将客户端连接托管给池,而池

spring中集成使用jedis(1)

本文主要说明在项目中通过spring集成jedis的使用方法. 首先,在jedis的连接配置中,出于性能考虑选择连接池形式.jedis中的连接池类为 redis.clients.jedis.JedisPool 其中包含多个构造器,可根据需要自行选取,这里选择较为基础形式: public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, final int port) { this(poolConfig,

不使用SpringBoot如何将原生Feign集成到Spring中来简化http调用

在微服务架构中,如果使用得是SpringCloud,那么只需要集成SpringFeign就可以了,SpringFeign可以很友好的帮我们进行服务请求,对象解析等工作. 然而SpingCloud是依赖于SpringBoot的.在老的Spring项目中通常是没有集成SpringBoot,那么我们又该如何使用Feign组件进行调用呢? 这种情况下就只能使用原生Feign了,Feign使用手册:https://www.cnblogs.com/chenkeyu/p/9017996.html 使用原生Fe

在Spring中集成shiro

1.在pxm添加集成导入 <!-- shiro的支持包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.4.0</version> <type>pom</type> </dependency> <!-- shiro与Spr

Spring中集成Shiro授权实例

授权流程回顾 首先说一句,使用授权的前提当然是先要实现身份验证,也就是要保证用户登录之后才可能考虑授权的问题.关于身份验证之前已经写过了,还不清楚的童鞋可以点这里 上一篇文章介绍了Shiro中授权的一些基础知识和原理.学了就要用,本篇文章就介绍如何在项目中应用Shiro的授权.这里为了方便大家阅读,先贴出上一篇文章中分析出的授权流程: 当我们调用Subject.hasRole(...)后 首先会委托给securityManager来处理,而securityManager内部有一个Authoriz

spring中 context:property-placeholder 导入多个独立的配置文件

spring中 context:property-placeholder 导入多个独立的 .properties配置文件? Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceh