我们上班时数据库工作正常。该数据库第二天开始工作时将引发异常。例外如下:
{"status":400,"message":"system error,
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 55,391,306 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
### The error may exist in file [D:\4-tomcat-8.5.24\webapps\devapi\WEB-INF\classes\mybatis\shipper\channel\ChannelMapper.xml]
### The error may involve com.money56.tdcs.api.module.shipper.channel.dao.ChannelDao.get-Inline
### The error occurred while setting parameters
### SQL: SELECT a.channel_id, a.resource_name, a.company_address, a.licence_num, a.is_open_position, a.link_name, a.link_mobile, a.status, a.create_time, a.update_time, a.remarks FROM pa_channel_base a WHERE a.channel_id = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 55,391,306 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
; SQL []; Communications link failure
The last packet successfully received from the server was 55,391,306 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 55,391,306 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago."}
原因是当长时间没有连接时,数据库连接池中的连接将无效,而再次连接时,将引发异常。
因此,我找到了一个解决方案,将testOnBorrow设置为true,以便每次将其从连接池中取出并准备使用时,都将测试当前使用情况是否可用。如果不可用,它将自动删除。
但是,此方法消耗性能。那么每个人都有更好的方法吗?