TransactionException: Error configuring AutoCommit

来源:互联网 发布:php在线升级原理 编辑:程序博客网 时间:2024/06/06 15:47

事务异常:自动提交配置错误. TransactionException: Error configuring AutoCommit

参考文章:

  1. MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  2. jdbc连接超时解决
  3. Mybatis连接池及相关配置说明

错误信息:

### The error may exist in com/xxx/message/mapper/MasMsgMapper.xml### The error may involve com.xxx.message.mapper.MasMsgMapper.getMasterMsgList### The error occurred while executing a query### Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false.  Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureLast packet sent to the server was 1 ms ago.[DEBUG] [2016-04-18 11:09:12] [method:org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:120)]Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@55eff3af][DEBUG] [2016-04-18 11:09:12] [method:org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(JdbcTransaction.java:125)]Error resetting autocommit to true before closing the connection.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Connection.close() has already been called. Invalid operation in this state.[DEBUG] [2016-04-18 11:09:12] [method:org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(JdbcTransaction.java:88)]Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@55eff3af][DEBUG] [2016-04-18 11:09:12] [method:org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(PooledDataSource.java:350)]A bad connection (1441788847) attempted to return to the pool, discarding connection.

项目中遇到这个问题,看了下日志发现这个异常.谷歌之后,大家都认同说是数据库连接超时的问题.
查询mysql的配置发现默认超时为2分钟,在页面上平均也是两分钟左右出现不正常的情况.
这里写图片描述
根据引用参考上面的文章得知:
在数据库设置的wait_timeout的时间里,如果数据库连接一直处于等待状态,然后到达这个时间后就会发生错误,mysql就会关闭数据库连接,通过上面的日志可以看到Error resetting autocommit to true before closing the connection.以及Connection.close() has already been called. Invalid operation in this state.这说明我们的操作是在连接关闭之后,所以会出现这样的问题.

国外的回答:

The last packet sent successfully to the server was 57,193,128 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.To solve this problem you need to set the poolPingQuery and poolPingEnabled properties in the iBatis config file. For example, see lines 11-13 in code below.最后一个包成功发送到服务器是在57,193,128毫秒之前,这个值超过了服务端配置的'wait_timeout'值.在使用应用程序之前你应该考虑失效或测试连接的有效性,针对客户端超时应增加服务器端配置的值,或者配置连接/J 连接的'autoReconnect=true'属性来解决这个问题.("jdbc:mysql://localhost:3306/cms_new?useUnicode=true&characterEncoding=UTF8&autoReconnect=true")解决这个问题你需要在你的iBatis配置文件中配置poolPingQuery 和 poolPingEnabled 属性,例如,看下面11-13行的代码.英文渣, 翻译的不好.. 各位凑合看.

解决方法

问题是出现在时间上面,时间过短?那么我们可以加大时间,mysql的最大超时时间分别是:windows:24天,linux:365天.设置方法请见http://zeusami.iteye.com/blog/1112827或网上了解.

你可能会想到在tomcat的数据源配置中有没有办法解决?的确,在jdbc连接url的配置中,你可以附上“autoReconnect=true”,但这仅对mysql5以前的版本起作用。增加“validation query”似乎也无济于事。

在参考了其他的文章之后,发现我们可以在mybatis的设置中通过配置以下三个属性来解决该问题.(所以我们可以同时配置属性也可以在数据库那里进行设置,这样子双保险.)

配置步骤:

首先通过配置侦测查询语句来让检测连接是否正常,value值则表示侦测的SQL语句(越简单越好).
然后设置间隔时间,这里设置为3600000(单位毫秒)则表示每一个小时侦测一次.
最后设置为true,表示开启侦测.

<property name="poolPingQuery" value="SELECT NOW()" /><property name="poolPingConnectionsNotUsedFor" value="3600000"/><property name="poolPingEnabled" value="true" />

属性解释:

  • poolPingQuery: 发送到数据的侦测查询,用来检验连接是否处在正常工作秩序中并准备接受请求。默认是“NO PING QUERY SET”,这会导致多数数据库驱动失败时带有一个恰当的错误消息
  • poolPingEnabled: 是否启用侦测查询。若开启,也必须使用一个可执行的 SQL 语句设置 poolPingQuery 属性(最好是一个非常快的 SQL),默认值:false。
  • poolPingConnectionsNotUsedFor: 配置 poolPingQuery 的使用频度。这可以被设置成匹配标准的数据库连接超时时间,来避免不必要的侦测,默认值:0(即所有连接每一时刻都被侦测 — 当然仅当
    poolPingEnabled 为 true 时适用)。

其他连接池的属性,见:http://www.letiantian.me/2015-01-14-learn-mybatis-from-scratch-13/

配置信息:

<environments default="development">    <environment id="development">        <!-- 使用JDBC管理事务 -->        <transactionManager type="JDBC" />        <dataSource type="POOLED">            <property name="driver" value="com.mysql.jdbc.Driver" />            <property name="url" value="jdbc:mysql://127.0.0.1:3306/messages?useUnicode=true&amp;characterEncoding=UTF8&amp;autoReconnect=true" />            <property name="username" value="root" />            <property name="password" value="****" />            <property name="poolPingQuery" value="SELECT NOW()" />            <property name="poolPingEnabled" value="true" />            <property name="poolPingConnectionsNotUsedFor" value="3600000"/>        </dataSource>    </environment></environments>
1 0