iBatis org.apache.ibatis.transaction.TransactionException – Error configuring AutoCommit
最近发现用mybatis实现的登录功能,偶尔会爆出如下异常:
<pre class="brush:java prettyprint linenums">
- 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... ...
</pre>
多试几次以后,就又正常了,这个异常不是一直存在。不得其解,google 之,得到一种解决方案,不过他遇到的情况发生在mysql重启之时。
源地址: http://burtsev.net/en/2012/01/12/100
解决此问题,需要在mybatis(iBatis)的配置文件中加入两个属性:poolPingQuery 和 poolPingEnabled。具体如下:
<pre class="brush:xml prettyprint linenums">
-
<?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>
</pre>
其中SELECT id FROM user WHERE id = 1是查询配置中的数据库的某个表,这里是user表,推测这里可以配置成任意一个可以执行的sql语句,当然越简单越好。
网友评论