SQL 会话在夜间流量较少时开始失败

问题描述 投票:0回答:1

我的团队面临一个问题,即我们的 SQL 会话在夜间流量较少时无法

insert
/
update
/
delete

我们的代码在白天运行完美。晚上发生的情况是我们能够得到一个

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 并被终止?

mysql sql spring mybatis
1个回答
0
投票

通过在使用之前调用

conn.pingConnection()
解决了这个问题。如果它已经过时,我们将完全从工厂重新初始化连接池

https://mybatis.org/mybatis-3/configuration.html

自从我弄清楚这一点以来已经快三年了。时钟滴答作响,生命流逝ig

© www.soinside.com 2019 - 2024. All rights reserved.