[数据源] 使用p6spy 监控spring jdbcTemplate 的sql操作

第一步 加入p6spy jar


<dependency>
  <groupId>p6spy</groupId>
  <artifactId>p6spy</artifactId>
</dependency>
第二步 创建数据源



<bean id="dataSourceTarget" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
第三步 将第二步创建的数据源ref给p6spy

<bean id="dataSource" class="com.p6spy.engine.spy.P6DataSource">
      <constructor-arg ref="dataSourceTarget"></constructor-arg>
</bean>



第四步 spring jdbcTemplate 使用 p6spy的数据源

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"></property>
</bean>



第五步 将spy.properties复制到resources目录,记录日志

module.log=com.p6spy.engine.logging.P6LogFactory
#module.outage=com.p6spy.engine.outage.P6OutageFactory

# the hsqldb open source driver
realdriver=com.microsoft.sqlserver.jdbc.SQLServerDriver


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

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=p6spy- %d{yyyy-MM-dd HH:mm:ss} [%-5p] %l -> %m%n

#RollingFile
log4j.appender.RollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingFile.File=d:/jmw_logs/jmw-p6spy.log
log4j.appender.RollingFile.encoding=UTF-8
log4j.appender.RollingFile.datePattern='.'yyyy-MM-dd
log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern=p6spy- %d{yyyy-MM-dd HH:mm:ss} [%-5p] %l -> %m%n

log4j.logger.p6spy=INFO,Console, RollingFile



[完毕]

你可能感兴趣的:([数据源] 使用p6spy 监控spring jdbcTemplate 的sql操作)