當(dāng)我使用個人阿里云測試機時,在查看實時輸出日志時,我發(fā)現(xiàn)數(shù)據(jù)庫連接失敗后,服務(wù)器一直在嘗試重新連接。一開始認(rèn)為系統(tǒng)遭受了連續(xù)的攻擊,但是在重新啟動服務(wù)后,就不再出現(xiàn)不斷重連的情況了??匆韵螺敵鋈罩荆?/p>
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 28000java.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ù)庫連接池的提示,這里就想到可能是 druid 連接池的問題,然后去掉 druid maven 依賴后在請求接口就不會出現(xiàn)重連的問題。
在上圖源碼找到最后一行 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)建超過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 } } }
這是一個多線程的類,而 run 方法里面設(shè)置了沒有限制的 for 循環(huán) for (;;) {}, 而日志報錯定位的信息:
connection = createPhysicalConnection();
如果符合條件 errorCount > connectionErrorRetryAttempts && timeBetweenConnectErrorMillis > 0 會再次嘗試重連,先看一下這幾個參數(shù)的含義:
在 run 方法初始化時為零,每次連接失敗,會自動加1
connectionErrorRetryAttempts
連接錯誤重試次數(shù),默認(rèn)值為 1。
protected int connectionErrorRetryAttempts = 1;
timeBetweenConnectErrorMillis
連接間隔時間,單位毫秒。默認(rèn)值為 500。
protected volatile long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS; public static final long DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS = 500;
我們在連接數(shù)據(jù)庫失敗后,要不在里面 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 才會進(jìn)入到 條件內(nèi),才會中斷循環(huán)。下面是 application.properties 文件的配置:
spring.datasource.druid.connection-error-retry-attempts=3
以上就是springboot?druid數(shù)據(jù)庫連接池連接失敗后一直重連怎么解決的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進(jìn)程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號