springboot druid数据库连接池连接失败后一直重连的解决方法-亚博电竞手机版

目录

  • druid 重连原因
  • errorcount 错误次数
  • 总结

在使用个人阿里云测试机,在查询实时输出日志时,看到数据库连接失败后,服务器一直在重连服务器。开始以为是遭受重复攻击,后面把服务重启后,就没有出现一直重连的情况。看以下输出日志:

2022-02-09 11:04:58.896 error 16876 --- [eate-1550991149] com.alibaba.druid.pool.druiddatasource : create connection sqlexception, url: jdbc:mysql://47.98.67,98:1234/test?usessl=false&characterencoding=utf-8&servertimezone=utc, errorcode 1045, state 28000

java.sql.sqlexception: access denied for user 'root'@'113.90.123.76' (using password: yes)

at com.mysql.cj.jdbc.exceptions.sqlerror.createsqlexception(sqlerror.java:129) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.exceptions.sqlerror.createsqlexception(sqlerror.java:97) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.exceptions.sqlexceptionsmapping.translateexception(sqlexceptionsmapping.java:122) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.connectionimpl.createnewio(connectionimpl.java:835) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.connectionimpl.(connectionimpl.java:455) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.connectionimpl.getinstance(connectionimpl.java:240) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.mysql.cj.jdbc.nonregisteringdriver.connect(nonregisteringdriver.java:199) ~[mysql-connector-java-8.0.16.jar:8.0.16]

at com.alibaba.druid.filter.filterchainimpl.connection_connect(filterchainimpl.java:156) ~[druid-1.1.10.jar:1.1.10]

at com.alibaba.dhttp://www.cppcns.comruid.filter.stat.statfilter.connection_connect(statfilter.java:218) ~[druid-1.1.10.jar:1.1.10]

at com.alibaba.druid.filter.filterchainimpl.connection_connect(filterchainimpl.java:150) ~[druid-1.1.10.jar:1.1.10]

at com.alibaba.druid.pool.druidabstractdatasource.createphysicalconnection(druidabstractdatasource.java:1560) ~[druid-1.1.10.jar:1.1.10]

at com.alibaba.druid.pool.druidabstractdatasource.createphysicalconnection(druidabstractdatasource.java:1623) ~[druid-1.1.10.jar:1.1.10]

at com.alibaba.druid.pool.druiddatasource$createconnectionthread.run(druiddatasource.java:2468) ~[druid-1.1.10.jar:1.1.10]

注意上面一直有 druid 数据库连接池的提示,这里就想到可能是 druid 连接池的问题,然后去掉 druid maven 依赖后在请求接口就不会出现重连的问题。

druid 重连原因

在上图源码找到最后一行 druiddatasource.java:2468 定位到源码上,createconnectionthread 创建连接线程,看一下 createconnectionthread 源码:

public class createconnectionthread extends thread { public createconnectionthread(string name){ super(name); this.setdaemon(true); } public void run() { initedlatch.countdown(); long lastdiscardcount = 0; int errorcount = 0; for (;;) { // addlast try { lock.lockinterruptibly(); } catch (interruptedexception e2) { break; } long discardcount = druiddatasource.this.discardcount; boolean discardchanged = discardcount - lastdiscardcount > 0; lastdiscardcount = discardcount; try { boolean emptywait = true; if (createerror != null && poolingcount == 0 && !discardchanged) { emptywait = false; } if (emptywait && asyncinit && createcount < initialsize) { emptywait = false; } if (emptywait) { // 必须存在线程等待,才创建连接 if (poolingcount >= notemptywaitthreadcount // && !(keepalive && activecount poolingcount < minidle)) { empty.await(); } // 防止创建超过maxactive数量的连接 if (activecount poolingcount >= maxactive) { empty.await(); continue; } } } catch (interruptedexception e) { lastcreateerror = e; lasterrortimemillis = system.currenttimemillis(); if (!closing) { log.error("create connection thread interrupted, url: " jdbcurl, e); } break; } finally { lock.unlock(); } physicalconnectioninfo connection = null; try { connection = createphysicalconnection(); setfailcontinuous(false); } catch (sqlexception e) { log.error("create connection sqlexception, url: " jdbcurl ", errorcode " e.geterrorcode() ", state " e.getsqlstate(), e); errorcount ; if (errorcount > connectionerrorretryattempts && timebetweenconnecterrormillis > 0) { // fail over retry attempts setfailcontinuous(true); if (failfast) { lock.lock(); try { notempty.signalall(); } finally { lock.unlock(); } } if (breakafteracquirefailure) { break; } try { thread.sleep(timebetweenconnecterrormillis); } catch (interruptedexception interruptex) { break; } } } catch (runtimeexception e) { log.error("create connection runtimeexception", e); setfailcontinuous(true); continue; } catch (error e) { log.error("create connection error", e); setfailcontinuous(true); break; } if (connection == null) { continue; } boolean result = put(connection); if (!result) { jdbcutils.close(connection.getphysicalconnection()); log.info("put physical connection to pool failed."); } errorcount = 0; // reset errorcount } } }

这是一个多线程的类,而 run 方法里面设置了没有限制的 for 循环 for (;;) {}, 而日志报错定位的信息:

connection = createphysicalconnection();

如果符合条件 errorcount > connectionerrorretryattempts && timebetweenconnecterrormillis > 0 会再次尝试重连,先看一下这几个参数的含义:

errorcount 错误次数

在 run 方法初始化时为零,每次连接失败,会自动加1

connectionerrorretryattempts

连接错误重试次数,默认值为 1。

protected int connectionerrorretryattempts = 1;

timebetweenconnecterrormillis

连接间隔时间,单位毫秒。默认值为 500。

protected volatile long timebetweenconnecterrormillis = default_time_between_connect_error_millis; public static final long default_time_between_connect_error_millis = 500;

我们在连接数据库失败后,要不在里面 break 中断,其中有

if (breakafteracquirefailure) { break; }

将改 break-after-acquire-failure 设置成 true,在 application.properties 文件如下配置:

spring.datasource.druid.break-after-acquire-failure=true

如果想多尝试连接几次,需要设置 connection-error-retry-attempts ,当 errorcount 大于 connectionerrorretryattempts 才会进入到 条件内,才会中断循环。在 application.properties 文件如下配置:

spring.datasource.druid.connection-error-retry-attempts=3

总结

druid 数据库连接失败,是因为在使用多线程连接数据时使用了无限制循环连接,需要在连接失败中断连接,需要设置 break-after-acquire-failure 为 true。设置之后数据库连接不成功也不会不断的重试。如果要设置重连次数要设置 connection-error-retry-attempts。

到此这篇关于springboot druid数据库连接池连接失败后一直重连的解决方法的文章就介绍到这了,更多相关springboot druid连接池连接失败内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有亚博电竞手机版的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系亚博电竞手机版删除

最新文章

网站地图