如何记录JDBC连接活动?

如何记录JDBC连接活动?,第1张

如何记录JDBC连接活动?

log4jdbc
库可用于记录JDBC连接。将此库添加到POM-

<dependency>    <groupId>org.slf4j</groupId>    <artifactId>slf4j-log4j12</artifactId>    <version>1.6.4</version></dependency><dependency>    <groupId>org.lazyluke</groupId>    <artifactId>log4jdbc-remix</artifactId>    <version>0.2.7</version></dependency>

配置

log4j.properties
。修改日志记录选项以根据自己的需要调整详细信息级别。

log4j.rootLogger=INFO,R, FILElog4j.appender.R=org.apache.log4j.ConsoleAppenderlog4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern=%d{ISO8601} %5p %t %c - %m%nlog4j.appender.FILE=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=%d{ISO8601} %5p %t %c - %m%nlog4j.appender.FILE.File=/home/kshitiz/documents/tomcat.loglog4j.appender.FILE.DatePattern='.'yyyy-MM-dd# Application logging optionslog4j.logger.org.apache=ERRORlog4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUGlog4j.logger.MySQL=ERRORlog4j.logger.org.springframework=DEBUGlog4j.logger.jdbc.sqlonly=OFFlog4j.logger.jdbc.sqltiming=ERRORlog4jdbc.sqltiming.error.threshold=10log4j.logger.jdbc.audit=OFFlog4j.logger.jdbc.resultset=ERRORlog4j.logger.jdbc.connection=ALLlog4j.logger.jdbc.resultsettable=OFF# Setup vm levelslog4j.logger.vm.none=FATALlog4j.logger.vm.error=ERRORlog4j.logger.vm.warn=WARNlog4j.logger.vm.info=INFOlog4j.logger.vm.debug=DEBUG

配置数据源

<bean id="dataSource"destroy-method="close">        <property name="driverClass" value="net.sf.log4jdbc.DriverSpy" />        <property name="jdbcUrl" value="${db.jdbc.url}" />        <property name="user" value="${db.user}" />        <property name="password" value="${db.password}" />     <!--         these are C3P0 properties -->        <property name="acquireIncrement" value="3" />        <property name="minPoolSize" value="10" />        <property name="maxPoolSize" value="30" />        <property name="maxIdleTime" value="600" /></bean>

关于上述数据源配置的注意事项:

  1. 您通常的驱动程序类名称应替换为
    net.sf.log4jdbc.DriverSpy
  2. 现在,您的JDBC URL必须以开头
    jdbc:log4jdbc
    。因此,如果您使用Oracle,则JDBC URL现在将以开头
    jdbc:log4jdbc:oracle

现在,日志消息将出现在屏幕上-

2013-01-29 15:52:21,549 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.core.JdbcTemplate - Executing SQL query [SELECt id from emp]2013-01-29 15:52:21,558 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource2013-01-29 15:52:31,878  INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection opened  org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)2013-01-29 15:52:31,878 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections:  1 (1)2013-01-29 15:52:31,895  INFO http-bio-8080-exec-3 jdbc.connection - 1. Connection closed  org.apache.commons.dbcp.DelegatingConnection.close(DelegatingConnection.java:247)2013-01-29 15:52:31,895 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections:  none2013-01-29 15:52:41,950  INFO http-bio-8080-exec-3 jdbc.connection - 2. Connection opened  org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)2013-01-29 15:52:41,950 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections:  2 (1)2013-01-29 15:52:52,001  INFO http-bio-8080-exec-3 jdbc.connection - 3. Connection opened  org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)2013-01-29 15:52:52,002 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections:  2 3 (2)2013-01-29 15:53:02,058  INFO http-bio-8080-exec-3 jdbc.connection - 4. Connection opened  org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)2013-01-29 15:53:02,058 DEBUG http-bio-8080-exec-3 jdbc.connection - open connections:  2 3 4 (3)2013-01-29 15:53:03,403 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.core.BeanPropertyRowMapper - Mapping column 'id' to property 'id' of type int2013-01-29 15:53:04,494 DEBUG http-bio-8080-exec-3 org.springframework.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource

一些Mysql命令显示连接详细信息-

> show variables like '%timeout%';> show status like '%onn%';> show full processlist;


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020836.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存