2023年11月29日发(作者:)
springbootdruid数据库连接池连接失败后⼀直重连
在使⽤个⼈阿⾥云测试机,在查询实时输出⽇志时,看到数据库连接失败后,服务器⼀直在重连服务器。开始以为是遭受重复攻击,后⾯把服务重
启后,就没有出现⼀直重连的情况。看以下输出⽇志:
2022-02-09 11:04:58.896 ERROR 16876 --- [eate-1550991149] ataSource : create connection SQLException, url: jdbc:mysql://47.98.6
eption: Access denied for user 'root'@'113.90.123.76' (using password: YES)
at SQLException(:129) ~[:8.0.16]
at SQLException(:97) ~[:8.0.16]
at ateException(:122) ~[:8.0.16]
at NewIO(:835) ~[:8.0.16]
at tionImpl.
at tance(:240) ~[:8.0.16]
at t(:199) ~[:8.0.16]
at tion_connect(:156) ~[:1.1.10]
at tion_connect(:218) ~[:1.1.10]
at tion_connect(:150) ~[:1.1.10]
at PhysicalConnection(:1560) ~[:1.1.10]
&& asyncInit && createCount < initialSize) {
emptyWait = false;
}
if (emptyWait) {
continue;
}
boolean result = put(connection);
if (!result) {
(sicalConnection());
("put physical connection to pool failed.");
}
errorCount = 0; // reset errorCount
}
}
}
这是⼀个多线程的类,⽽ ⽅法⾥⾯设置了没有限制的 for 循环 , ⽽⽇志报错定位的信息:
runfor (;;) {}
connection = createPhysicalConnection();
如果符合条件 会再次尝试重连,先看⼀下这⼏个参数的含义:
errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0
errorCount 错误次数
在 run ⽅法初始化时为零,每次连接失败,会⾃动加1
connectionErrorRetryAttempts
连接错误重试次数,默认值为 1。
protected int connectionErrorRetryAttempts = 1;
tion-error-retry-attempts=3
总结
druid 数据库连接失败,是因为在使⽤多线程连接数据时使⽤了⽆限制循环连接,需要在连接失败中断连接,需要设置
break-after-acquire-failure
为 。设置之后数据库连接不成功也不会不断的重试。如果要设置重连次数要设置 。
trueconnection-error-retry-attempts
如果觉得⽂章对你有帮助的话,请点个赞吧!


发布评论