iBatis org.apache.ibatis.transaction.TransactionException – Error configuring AutoCommit

最近发现用mybatis实现的登录功能,偶尔会爆出如下异常:

org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: org.apache.ibatis.transaction.TransactionException: Error configuring AutoCommit.  
Your driver may not support getAutoCommit() or setAutoCommit(). Requested setting: false. 
Cause: com.mysql.jdbc.CommunicationsException: 
Communications link failure due to underlying exception... ...

多试几次以后,就又正常了,这个异常不是一直存在。不得其解,google 之,得到一种解决方案,不过他遇到的情况发生在mysql重启之时。

源地址: http://burtsev.net/en/2012/01/12/100

解决此问题,需要在mybatis(iBatis)的配置文件中加入两个属性:poolPingQuery 和 poolPingEnabled。具体如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <typeAliases>
        ...
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${db.driver}" />
                <property name="url" value="${db.url}" />
                <property name="username" value="${db.user}" />
                <property name="password" value="${db.password}" />
                <property name="poolPingQuery" value="SELECT id FROM user WHERE id = 1" />
                <property name="poolPingEnabled" value="true" />
            </dataSource>
        </environment>
    </environments>

</configuration>

其中SELECT id FROM user WHERE id = 1是查询配置中的数据库的某个表,这里是user表,推测这里可以配置成任意一个可以执行的sql语句,当然越简单越好。

http://cssor.com/ibatis-org-apache-ibatis-transaction-transactionexception-error-configuring-autocommit.html

参与评论

电子邮件地址不会被公开。 必填项已用*标注

*

全部分类
Books(4)code(7)database(6)html&css(24)java(11)JavaScript(48)jQuery(24)linux(20)python(1)React(1)share(1)soft(4)solution(53)thinking(17)vim(9)WordPress(8)前端优化(12)拓展(33)服务器(31)移动开发(4)自然(22)