首页 > 解决方案 > 当流量较少时,SQL 会话在晚上开始失败

问题描述

我的团队面临一个问题,即我们的 SQL 会话在夜间流量较少时insert失败。updatedelete

我们的代码在白天完美运行。晚上发生的事情是我们能够得到一个SQLsession,但是当我们对它运行查询时,它会失败。

我们的代码是这样的:

SqlSession sqlSession = openSession();
// successful in getting an open session
FooMapper = sqlSession.getMapper(FooMapper.class);
// Gets the mapper
FooMapper.insert(Foo);
// prepares the SQL statement successfully
sqlSession.commit()
// FAILS 

日志中的错误:

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 810,878 milliseconds ago. The last packet sent successfully to the server was 810,880 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.

MyBatis 属性:

"ssl", "false"
"poolPingQuery", "/* ping */ SELECT 1"
"poolPingEnabled", "true"
"poolPingConnectionsNotUsedFor", 20000 + ""
"poolMaximumActiveConnections", Integer.toString(25)
"poolMaximumIdleConnections", Integer.toString(10)
"allowPublicKeyRetrieval","true"

请帮忙

更新 1:我深入研究了服务器日志。基本上我们的 wait_timeout 设置为 500 秒,并且所有发生此错误的时间都超过 500 秒,所以我的理解是我们的 mysql 服务器正在杀死这些连接,但 mybatis 没有将它们从它的池中删除,这就是为什么会出现这个问题。但是我们无法理解,当我们每 20 秒对每个空闲连接进行 ping 操作时,连接如何空闲超过 wait_timeout 并被终止?

标签: mysqlsqlspringmybatis

解决方案


推荐阅读