当前位置:网站首页>Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception

Upgrade Alibaba cloud RDS (relational database service) instance to com mysql. jdbc. exceptions. Troubleshooting of jdbc4.communicationsexception

2022-07-07 08:59:00 bboyzqh

The phenomenon

Currently, alicloud is used in the marketing database MYSQL database , Need support in the near future DBA To Alibaba cloud RDS upgrade , The upgrade announcement is as follows :
 Insert picture description here
In the morning 5 spot 10 Start to report errors in the log after upgrading , The error message is as follows :
 Insert picture description here

Problem analysis and conclusion

In general duridCommunicationsException The reason is that the connection pool obtains the invalid connection . from mysql Come up and say :

Mysql Server default “wait_timeout” yes 8 Hours ( That is to say, the default value is 28800 second ), That is, a connection More leisure than 8 Hours ,Mysql Will automatically disconnect the connection, Generally speaking, it is a connection between 8 There was no activity for hours , Will automatically disconnect the connection . And the connection pool thinks that the connection is still valid ( Because the validity of the connection is not verified ), When the app requests to use the connection , Will lead to the above error .

from druid There are several properties on configuration properties and connection failures :

  • minEvictableIdleTimeMillis: The connection idle time is greater than this value and the free connection in the pool is greater than minIdle Then close the connection . Default values used in configuration 30 minute .
  • maxEvictableIdleTimeMillis: Connection idle time is greater than this value , No matter minIdle Close the connection . Default values used in configuration 7 Hours .
  • maxidle: at present druid It's abandoned maxidle attribute , Use maxActive and minIdel Instead of , Reference resources https: //github.com/alibaba/druid/wiki/FAQ, from druid In terms of configuration ,minIdle yes 50,maxActive yes 400( Unreasonable code setting , Adjustable ).

In theory druid By default, the maximum number of connection failures 7 An hour is less than the database 8 Hours , There will be no failed connections at the configuration level . And from the announcement , Have a show :“ In the process of migration, there will be 30 Seconds of connection ”, explain rds After the flash, the original database connection It's not working , and smc In the system druid The connection pool is still valid , So it leads to com.mysql.jdbc.exceptions.jdbc4. CommunicationsException happen .

Solution

Direct thinking in druid The thread gets the connection and checks it ok 了 , Related attributes are test- WhileIdle、testOnBorrow, The meaning is as follows :

  • testWhileIdle: If true( Default true), When an application applies to the connection pool for a connection , also testOnBorrow by false when , The connection pool will determine if the connection is idle , If it is , Verify that the connection is available ( Efficient , Recommended ).
  • testOnBorrow:( Default false) Check the connection validity when applying for connection ( Inefficient , Affect performance ), And testWhileIdle It's almost the same , It is to test the validity of the connection when obtaining the connection , If both are true, be testOnBorrow first High class , You will not use testWhileIdle.
  • validationQuery: The query used to verify the connection before the connection pool returns the connection to the caller SQL, A query statement is required ( Turn on testWhileIdle You have to set validationQuery).

druid Add... To the configuration :

spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.validation-query=SELECT 1

There's another question , Without configuring the above properties ,testWhileIdlel The default is true,testOnBorrow Default by false,validationQuery The default is empty. , Why is it not mandatory validationQuery Well ? At this time, we should divide 2 There are three stages to explain the problem :

  • durid Initialization phase : Played only error, The initialization process is not interrupted
  • Get the connection process : Check the minEvictableIdleTimeMillis and maxEvictableIdleTimeMillis, Find out validation- Query by null Straight back , So the above configuration only needs to add validationQuery The configuration of can also solve the problem .

Welcome to WeChat official account. : Fang Chen's blog
 Insert picture description here

原网站

版权声明
本文为[bboyzqh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130629348669.html