Druid连接池慢SQL⽇志记录
时间 2014-08-27 13:02:05
原⽂
主题
由于⼯作原因,这⾥先不说连接数据库部分的配置,后⾯会补上,直接进⼊慢SQL⽇志记录。
l中增加如下配置:
<bean abstract="true" id="mysql_database" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 监控统计拦截的filters,⽇志记录 edited by zhangpl 20140824 start -->
<!-- 并在filters属性中配置了log4j -->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
<ref bean="log-filter" />
</list>
</property>
<!-- 监控统计拦截的filters,⽇志记录 edited by zhangpl 20140824 end -->
<property name="filters">
<value>stat,wall,log4j</value>
</property>
<!-- 其它基本配置略...-->
</bean>
<!-- 慢SQL记录 edited by zhangpl 20140824 start-->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="mergeSql" value="true" />
<property name="slowSqlMillis" value="1000" />
<property name="logSlowSql" value="true" />
</bean>
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
<!-- <property name="resultSetLogEnabled" value="false" /> -->
<!-- <property name="statementExecutableSqlLogEnable" value="true" /> -->
</bean>
<!-- 慢SQL记录 edited by zhangpl 20140824 end-->
2.log4j.properties中配置如下:
#1.打印druid连接池SQL语句,info级别的语句打印到info_slowsql.log,warn级别语句打印到warn_slowlog.log;
#2.将⽇志写到⽂件中;
# Druid
log4j.logger.druid.sql=info,A1,A2
log4j.logger.druid.sql.DataSource=info,A1,A2
log4j.logger.druid.sql.Connection=info,A1,A2
log4j.logger.druid.sql.Statement=info,A1,A2
log4j.logger.druid.sql.ResultSet=info,A1,A2
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=../druid-logs/info_slowsql.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
#log4j.appender.A1.File=${catalina.home}/logs/info_slowsql.log
#log4j.appender.A1.File=${WebApp.TomcatHome}/logs/info_slowsql.log
log4j.appender.A1.ImmediateFlush=true
log4j.appender.A1.Append=true
log4j.appender.A1.Threshold=info
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.File=../druid-logs/warn_slowsql.log
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
#log4j.appender.A2.File=${catalina.home}/logs/warn_slowsql.log
#log4j.appender.A2.File=${WebApp.TomcatHome}/logs/warn_slowsql.log
log4j.appender.A2.ImmediateFlush=true
log4j.appender.A2.Append=true
log4j.appender.A2.Threshold=warn
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] [%c] - %m%n
经过上⾯的配置,就会在⽂件中打印,如下的慢SQL语句:
......
[2014-08-26 11:12:59] [ERROR] [com.alibaba.druid.filter.stat.StatFilter] - slow sql 3121 millis.
select count(1) from person where schoolid=? and isDeleted=0and GET_FIRST_PINYIN_CHAR(username) =?
["145","a"]
......
有问题欢迎指正,可参考官⽅⽂档,以官⽅⽂档为准。
过程中遇到⽇志报错“
Communications link failure,The last packet successfully received from the server
was *** millisecon
”
使⽤Connector/J连接MySQL数据库,程序运⾏较长时间后就会报以下错误:
Communications link failure,The last packet successfully received from the server was * **millisecond ago.
sql自学难吗The last packet successfully sent to the server was * **millisecond ago。
其中错误还会提⽰你修改wait_timeout或是使⽤Connector/J的autoReconnect属性避免该错误。
后来查了⼀些资料,才发现遇到这个问题的⼈还真不少,⼤部分都是使⽤连接池⽅式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:
按照错误的提⽰,可以在JDBC URL中使⽤autoReconnect属性,实际测试时使⽤了autoReconnect=true&failOverReadOnly=false,不
过并未起作⽤,使⽤的是5.1版本,可能真像⽹上所说的只对4之前的版本有效。
没办法,只能修改MySQL的参数了,wait_timeout最⼤为31536000即1年,在myf中加⼊:
[mysqld] wait_timeout=31536000 interactive_timeout=31536000
重启⽣效,需要同时修改这两个参数。
Caused by: ptions.jdbc4.CommunicationsException: Communications link failure [09:49:36.821] [09:49:36.821]Last packet sent to the server
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论