一个定时任务每天晚上三点执行一遍,第一天没问题,第二天就开始出问题了,日志报这个错误,
### The error may exist in mapper/LogMapper.xml
### The error may involve mapper.LogMapper.getHotelMessage
### 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:
The last packet successfully received from the server was86394 milliseconds ago.
The last packet sent successfully to the server was 86394 milliseconds ago,
which 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.
百度后说是mysql 有一个侦测的机制,8小时的连接没有动作,就会认为是死的,就会出现以上错误,所以要配置一个定时查询,小于8小时,最好简单一点,我用的mybatis 有一个配置直接配上就行了。
在jdbc 的url 中开启 autoReconnect=true
在
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<dataSource type="POOLED">
<property name="url" value="${cp.Url}"/>
<property name="driver" value="${cp.DriverClassName}"/>
<property name="username" value="${cp.Username}"/>
<property name="password" value="${cp.Password}"/>
<property name="poolPingEnabled" value="true"/>
<property name="poolPingQuery" value="select 1"/>
<property name="poolPingConnectionsNotUsedFor" value="3600000"/>
</dataSource>
</environment>
</environments>
poolPingEnabled 、 poolPingQuery 、 poolPingConnectionsNotUsedFor 开启就可以了,
然后我在配置这个url 的时候xml 提示报错,就是以下的问题 有一个转义的问题
The reference to entity "autoReconnect" must end with the ';' delimiter.这个是因为xml中有转移字符
错误的写法
value="jdbc:mysql://127.0.0.1:3306/CCCXXX?characterEncoding=utf-8&autoReconnect=true" />
正确的写法
value="jdbc:mysql://127.0.0.1:3306/72byte_cloud?characterEncoding=utf-8&autoReconnect=true" />
xml中的转义字符
< | < | 小于号 |
> | > | 大于号 |
& | & | 和 |
' | ' | 单引号 |
" | " |
参考https://blog.csdn.net/hanye0204856/article/details/12998477