druid,log4j记录sql,发现会执行三次,

jack_jones 发布于 2014/10/27 16:55
阅读 5K+
收藏 1
2014-10-27 16:51:30,146 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} created. select count(id) from User>
2014-10-27 16:51:30,146 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} created. select count(id) from User>
2014-10-27 16:51:30,146 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} created. select count(id) from User>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Parameters : []>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Parameters : []>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Parameters : []>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Types : []>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Types : []>
2014-10-27 16:51:30,147 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} Types : []>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. 1.089623 millis. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. 1.089623 millis. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} executed. 1.089623 millis. select count(id) from User>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} closed>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} closed>
2014-10-27 16:51:30,148 DEBUG [druid.sql.Statement] - <{conn-10002, pstmt-20001} closed>


我访问了一次,从日志看,执行三次sql语句,怎么回事?

加载中
0
心有灵犀
心有灵犀
解决没有啊?求分享一下,
0
lgscofield
lgscofield
应该是log4j配置的问题吧
0
jack_jones
jack_jones

引用来自“lgscofield”的评论

应该是log4j配置的问题吧
#druid日志
log4j.logger.druid.sql=error,appendConsole
log4j.logger.druid.sql.DataSource=error,appendConsole
log4j.logger.druid.sql.Connection=error,appendConsole
log4j.logger.druid.sql.Statement=debug,appendConsole

log4j.logger.druid.sql.ResultSet=error,appendConsole


我是这么配的

0
罗厚付
罗厚付

是不是同时配置了filter和proxyFilters

#过滤   如果同时配置了filters和proxyFilters,是组合关系,并非替换关系
#filters:config

建议在properties就配置个filters:config,用来加密数据库连接密码。然后在datasource里配置

<!-- 配置监控统计拦截的filters -->
<property name="proxyFilters">
<list>
<ref bean="stat-filter" />
<ref bean="wall-filter" />
<ref bean="log-filter" />
</list>
</property>

jack_jones
jack_jones
只配置了proxyFilters,没有filters
0
罗厚付
罗厚付
把配置以发出来
0
jack_jones
jack_jones

引用来自“echard”的评论

把配置以发出来
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
     
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  
    <!-- 基本属性 url、user、password -->
    <property name="url" value="jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&amp;characterEncoding=utf-8&amp;zeroDateTimeBehavior=convertToNull" />
    <property name="username" value="root" />
    <property name="password" value="123456789" />
  
    <!-- 配置初始化大小、最小、最大 -->
    <property name="initialSize" value="2" />
    <property name="minIdle" value="3" />
    <property name="maxActive" value="20" />
    
    <!--在内置监控页面weburi-detail.html中,查看JdbcPoolConnectionOpenCount和JdbcPoolConnectionCloseCount属性,如果不相等,就是泄漏了。 -->
 <property name="removeAbandoned" value="true" /> <!-- 打开removeAbandoned功能 -->
    <property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
    <property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
  
    <!-- 配置获取连接等待超时的时间 -->
    <property name="maxWait" value="60000" />
  
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
  
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="300000" />
  
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
  
    <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->
  
    <!-- 配置监控统计拦截的filters -->
<!-- 	  <property name="filters" value="wall" /> -->
 <property name="proxyFilters">
 <list>
 <ref bean="stat-filter"/>
 <ref bean="wall-filter"/>
 <ref bean="log-filter"/>
 </list>
 </property>
 </bean>
 
 <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
 <property name="mergeSql" value="true" />
 <property name="slowSqlMillis" value="3000" />
 <property name="logSlowSql" value="true" />
 </bean> 
 <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
 <property name="dbType" value="mysql" />
 <property name="logViolation" value="true"/>
 <property name="throwException" value="false"/>
 </bean>
 <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
 <property name="statementExecutableSqlLogEnable" value="false" />
 <property name="resultSetLogEnabled" value="false" />
 </bean>
 
</beans>  


log4j.properties:

#根针对所有的日志包
log4j.rootLogger = error , appendConsole
#下面两个配置是指定包的特殊处理,针对两个指定的日志级别,如果不指定的话则使用父日志记录器(rootLogger)的,指定了就覆盖掉了父日志记录器的
#设置dao包的日志配置
#log4j.logger.com.xun.log4j.dao = debug, appendDao
#设置service包的日志配置
#log4j.logger.com.xun.log4j.service = info, appendService

#druid日志
log4j.logger.druid.sql=error,appendConsole
log4j.logger.druid.sql.DataSource=error,appendConsole
log4j.logger.druid.sql.Connection=error,appendConsole
log4j.logger.druid.sql.Statement=debug,appendConsole
log4j.logger.druid.sql.ResultSet=error,appendConsole

#控制台的配置
log4j.appender.appendConsole = org.apache.log4j.ConsoleAppender
#Threshold:设置此appender的日志级别,这里会覆盖全局的(rootLogger中)定义的日志级别
log4j.appender.appendConsole.Threshold = debug
#设置日志输出编码方式为UTF-8,如果不指定,会以当前运行操作系统的编码方式记录
log4j.appender.appendConsole.encoding = UTF-8
log4j.appender.appendConsole.layout = org.apache.log4j.PatternLayout
log4j.appender.appendConsole.layout.ConversionPattern=%d %p [%c] - <%m>%n
 
#appendDao配置
log4j.appender.appendDao = org.apache.log4j.FileAppender
log4j.appender.appendDao.layout = org.apache.log4j.PatternLayout
log4j.appender.appendDao.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss}]--[%t] [%p] -%l -%m%n%n
log4j.appender.appendDao.Append = false
log4j.appender.appendDao.File = dao.txt
 
#appendService
log4j.appender.appendService = org.apache.log4j.FileAppender
log4j.appender.appendService.layout = org.apache.log4j.PatternLayout
log4j.appender.appendService.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss}]--[%t] [%p] -%l -%m%n%n
log4j.appender.appendService.Append = false
log4j.appender.appendService.File = service.txt




0
罗厚付
罗厚付
要druid配置
jack_jones
jack_jones
我是在spring里使用的druid,您说的配置是指??
0
罗厚付
罗厚付
先把log中druid的配置去了试试
0
Ss丶壮壮
Ss丶壮壮
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} created. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} created. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} created. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Parameters : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Parameters : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Parameters : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Types : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Types : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} Types : []
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002, rs-50002} query executed. 3.397767 millis. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002, rs-50002} query executed. 3.397767 millis. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002, rs-50002} query executed. 3.397767 millis. select logconfig0_.id as id22_, logconfig0_.create_date as create2_22_, logconfig0_.modify_date as modify3_22_, logconfig0_.action_class_name as action4_22_, logconfig0_.action_method_name as action5_22_, logconfig0_.description as descript6_22_, logconfig0_.operation_name as operation7_22_ from cf_log_config logconfig0_ order by logconfig0_.create_date desc
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} clearParameters. 
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} clearParameters. 
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} clearParameters. 
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} closed
<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} closed

<2015-04-14 10:46:08> <DEBUG> [druid.sql.Statement] - {conn-10001, pstmt-20002} closed



同求解决办法

0
Yoreay
Yoreay

把log4j里面的配置删除,spring里面配置的filter有log类型就可以了,还有就是sql不是执行了3次,而是打印了3遍

返回顶部
顶部