springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法
在使用個(gè)人阿里云測(cè)試機(jī),在查詢實(shí)時(shí)輸出日志時(shí),看到數(shù)據(jù)庫(kù)連接失敗后,服務(wù)器一直在重連服務(wù)器。開(kāi)始以為是遭受重復(fù)攻擊,后面把服務(wù)重啟后,就沒(méi)有出現(xiàn)一直重連的情況。看以下輸出日志:
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.<init>(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.druid.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 數(shù)據(jù)庫(kù)連接池的提示,這里就想到可能是 druid 連接池的問(wèn)題,然后去掉 druid maven 依賴后在請(qǐng)求接口就不會(huì)出現(xiàn)重連的問(wèn)題。
druid 重連原因
在上圖源碼找到最后一行 DruidDataSource.java:2468 定位到源碼上,CreateConnectionThread 創(chuàng)建連接線程,看一下 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) { ? ? ? ? ? ? ? ? ? ? ? ? // 必須存在線程等待,才創(chuàng)建連接 ? ? ? ? ? ? ? ? ? ? ? ? if (poolingCount >= notEmptyWaitThreadCount // ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? && !(keepAlive && activeCount + poolingCount < minIdle)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? empty.await(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? // 防止創(chuàng)建超過(guò)maxActive數(shù)量的連接 ? ? ? ? ? ? ? ? ? ? ? ? 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 ? ? ? ? ? ? } ? ? ? ? } ? ? }
這是一個(gè)多線程的類,而 run 方法里面設(shè)置了沒(méi)有限制的 for 循環(huán) for (;;) {}, 而日志報(bào)錯(cuò)定位的信息:
connection = createPhysicalConnection();
如果符合條件 errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0 會(huì)再次嘗試重連,先看一下這幾個(gè)參數(shù)的含義:
errorCount 錯(cuò)誤次數(shù)
在 run 方法初始化時(shí)為零,每次連接失敗,會(huì)自動(dòng)加1
connectionErrorRetryAttempts
連接錯(cuò)誤重試次數(shù),默認(rèn)值為 1。
protected int connectionErrorRetryAttempts = 1;
timeBetweenConnectErrorMillis
連接間隔時(shí)間,單位毫秒。默認(rèn)值為 500。
protected volatile long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS; public static final long DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS = 500;
我們?cè)谶B接數(shù)據(jù)庫(kù)失敗后,要不在里面 break 中斷,其中有
if (breakAfterAcquireFailure) { break; }
將改 break-after-acquire-failure 設(shè)置成 true,在 application.properties 文件如下配置:
spring.datasource.druid.break-after-acquire-failure=true
如果想多嘗試連接幾次,需要設(shè)置 connection-error-retry-attempts ,當(dāng) errorCount 大于 connectionErrorRetryAttempts 才會(huì)進(jìn)入到 條件內(nèi),才會(huì)中斷循環(huán)。在 application.properties 文件如下配置:
spring.datasource.druid.connection-error-retry-attempts=3
總結(jié)
druid 數(shù)據(jù)庫(kù)連接失敗,是因?yàn)樵谑褂枚嗑€程連接數(shù)據(jù)時(shí)使用了無(wú)限制循環(huán)連接,需要在連接失敗中斷連接,需要設(shè)置 break-after-acquire-failure 為 true。設(shè)置之后數(shù)據(jù)庫(kù)連接不成功也不會(huì)不斷的重試。如果要設(shè)置重連次數(shù)要設(shè)置 connection-error-retry-attempts。
到此這篇關(guān)于springboot druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連的解決方法的文章就介紹到這了,更多相關(guān)springboot druid連接池連接失敗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中反射reflect的基礎(chǔ)知識(shí)講解
這篇文章主要介紹了Java中反射reflect的基礎(chǔ)知識(shí)講解,Java中的反射,它算是Java當(dāng)中非常底層的一個(gè)技術(shù),平時(shí)我們我們用得不多,實(shí)際上它也的確非常復(fù)雜同時(shí)也難以理解,但是涉及到底層的東西Java都給我們封裝好了,我們直接拿來(lái)調(diào)用即可,需要的朋友可以參考下2023-10-10SpringBoot+jpa配置如何根據(jù)實(shí)體類自動(dòng)創(chuàng)建表
這篇文章主要介紹了SpringBoot+jpa配置如何根據(jù)實(shí)體類自動(dòng)創(chuàng)建表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
JdbcTemplate是Spring框架中的一個(gè)核心類,用于簡(jiǎn)化Java應(yīng)用程序與關(guān)系型數(shù)據(jù)庫(kù)的交互操作,本文給大家介紹SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的方法,感興趣的朋友跟隨小編一起看看吧2023-10-10關(guān)于Java中的繼承和組合的一個(gè)錯(cuò)誤使用的例子
這篇文章主要介紹了關(guān)于Java中的繼承和組合的一個(gè)錯(cuò)誤使用的例子,需要的朋友可以參考下2016-08-08MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式
這篇文章主要介紹了MAVEN_HOME、M2_HOME,maven環(huán)境變量設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Java構(gòu)造函數(shù)與普通函數(shù)用法詳解
本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識(shí)點(diǎn),對(duì)此有興趣的朋友可以參考學(xué)習(xí)下。2018-03-03java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法
這篇文章主要介紹了java使用JDBC動(dòng)態(tài)創(chuàng)建數(shù)據(jù)表及SQL預(yù)處理的方法,涉及JDBC操作數(shù)據(jù)庫(kù)的連接、創(chuàng)建表、添加數(shù)據(jù)、查詢等相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08