MySQL validatequery_mysql – 陈旧的连接,validationQuery无法修复

我得到了可怕的

MySQL JDBC陈旧连接异常:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 243,263,541 milliseconds ago. The last packet sent successfully to the server was 243,263,541 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

似乎每个人都同意这是通过使用validationQuery testOnBorrow来解决的,但这并不能解决问题.

我使用以下软件

MySQL 5.1.41-3ubuntu12.10

Connector / J 5.1.18

Tomcat 6.0.24

以下是如何在server.xml中定义连接,我们使用tomcat-dbcp来连接池.

auth="Container"

driverClassName="com.mysql.jdbc.Driver"

factory="org.apache.commons.dbcp.BasicDataSourceFactory"

logAbandoned="true"

maxActive="75"

maxIdle="20"

maxWait="10000"

name="jdbc/jndiname"

password="password"

removeAbandoned="true"

removeAbandonedTimeout="60"

validationQuery="/* ping */SELECT 1"

testOnBorrow="true"

testOnReturn="true"

timeBetweenEvictionRunsMillis="10000"

testWhileIdle="true"

scope="Shareable"

type="javax.sql.DataSource"

url="jdbc:mysql://host:3306/schema"

username="username" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现该问题可能是因为 MyBatis 对于 MySQL 的 Merge 语法的支持不太好。你可以尝试在 MyBatis 的配置文件中加入下面的配置: ```xml <settings> <setting name="useGeneratedKeys" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="jdbcTypeForNull" value="OTHER"/> <setting name="safeRowBoundsEnabled" value="false"/> <setting name="aggressiveLazyLoading" value="false"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useCacheStatements" value="true"/> <setting name="cacheEnabled" value="true"/> <setting name="defaultStatementTimeout" value="25000"/> <setting name="defaultFetchSize" value="100"/> <setting name="lazyLoadingEnabled" value="false"/> <setting name="callSettersOnNulls" value="false"/> <setting name="logImpl" value="LOG4J"/> <setting name="configurationFactory" value="com.xxx.xxx.MybatisConfigurationFactory"/> <setting name="defaultScriptingLanguage" value="com.xxx.xxx.CustomLanguageDriver"/> </settings> ``` 其中,`CustomLanguageDriver` 是自定义的 MyBatis 语言驱动,可以通过继承 `XMLLanguageDriver` 实现。在自定义的语言驱动中,你可以重写 `merge()` 方法来实现 MySQL 的 Merge 语法。 另外,你还需要在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 这是集成了 Druid 连接池的依赖。你需要在 MyBatis 的配置文件中配置 Druid 连接池。例如: ```xml <dataSource type="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="maxActive" value="20"/> <property name="initialSize" value="1"/> <property name="maxWait" value="60000"/> <property name="minIdle" value="1"/> <property name="validationQuery" value="SELECT 1 FROM DUAL"/> <property name="testOnBorrow" value="false"/> <property name="testOnReturn" value="false"/> <property name="testWhileIdle" value="true"/> <property name="timeBetweenEvictionRunsMillis" value="60000"/> <property name="minEvictableIdleTimeMillis" value="25200000"/> <property name="filters" value="stat,wall"/> <property name="connectionProperties" value="druid.stat.mergeSql=true"/> </dataSource> ``` 注意,最后一行的 `druid.stat.mergeSql=true` 是用来开启 Druid 的 Merge SQL 统计功能的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值