本人微信公众号"aeolian"~

Mysql Cannot get a connection, pool error Timeout waiting for idle object

问题

Cannot get a connection, pool error Timeout waiting for idle object

《Mysql Cannot get a connection, pool error Timeout waiting for idle object》

原因

db.properties中配置的maxWait为连接池获取mysql连接最大等待时间,超过了这个时间仍然没获取到就会报这个错。

《Mysql Cannot get a connection, pool error Timeout waiting for idle object》

深入分析一下为啥会获取不到Mysql连接,手动用来浏览器请求发送,发现当连接达到MaxActive时会获取不到,而剩下的50个都在Sleep状态,原来是用完后没有回收导致的。

《Mysql Cannot get a connection, pool error Timeout waiting for idle object》

原因1

应用程序没有很好的关闭使用后的连接

方案1:

请检查自己的应用程序是否正确关闭了数据库连接,注意一定要放到finally中关闭。(不推荐,让数据库连接池去管理连接)

方案2:

如果确实无法排查出具体哪些代码没有关闭数据库连接,可以通过配置参数完成自动回收,并记录回收日志,以便于定位问题代码;tomcat中连接池的配置自动回收参数为:removeAbandoned、 removeAbandonedTimeout、logAbandoned三个。(推荐)

#验证使用的SQL语句
validationQuery SELECT 1
#池中的连接空闲30分钟后被回收
minEvictableIdleTimeMillis 18000
#每30秒运行一次空闲连接回收器
timeBetweenEvictionRunsMillis 10000
#借出连接时不要测试,否则很影响性能
testOnBorrow false
testWhileIdle true

#程序中的连接不使用后是否被连接池回收
#DBCP 2.0.1 (该版本要使用removeAbandonedOnMaintenance和removeAbandonedOnBorrow)
#removeAbandoned=true
removeAbandonedOnMaintenance=true
removeAbandonedOnBorrow=true
#数据库连接过多长时间不用将被视为被遗弃而收回连接池中(单位秒)。(为配合测试程序才配置为30秒) 
removeAbandonedTimeout=30

mysql等待时间要设为8小时,连接全部交给连接池管理。

DBCP官网:https://commons.apache.org/proper/commons-dbcp/configuration.html

原因2

应用压力过大,确实无法获取空闲连接

方案1:

这种情况可以调整maxActive、maxIdle、maxWait等连接池的容量和超时限制等参数以获取更大的连接池容量和等待时间。

 

点赞
  1. Alex says:

    Thanks in favor of sharing such a fastidious idea, piece of writing
    is fastidious, thats why i have read it fully

  2. Alex says:

    Unquestionably consider that which you stated. Your favorite justification seemed to be on the internet the easiest thing to remember of.
    I say to you, I certainly get annoyed at the same time as other folks consider
    issues that they just do not know about. You controlled to hit the nail upon the highest and also
    defined out the whole thing without having side effect , folks could take a signal.
    Will likely be back to get more. Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *