In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the springboot druid database connection pool connection failure has been reconnected after the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that everyone after reading this springboot druid database connection pool connection failure has been reconnected how to solve the article will have a harvest, let's take a look at it.
When using the personal Aliyun test machine, when querying the real-time output log, the server has been reconnecting to the server after seeing that the database connection failed. At first, I thought it was a repeated attack, but after the service was restarted, there was no constant reconnection. Look at the following output log:
2022-02-09 11 14 ERROR 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.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]
Note that there is always a prompt for druid database connection pooling. Here we think that it may be the problem of druid connection pooling, and then after removing the druid maven dependency, there will be no reconnection problem in the request interface.
Reasons for druid reconnection
Find the last line of DruidDataSource.java:2468 in the source code above and locate it on the source code. CreateConnectionThread creates a connection thread and take a look at the CreateConnectionThread source code:
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}
This is a multi-threaded class, and the run method sets the unlimited for loop for (;;) {}, and the log reports the error location information:
Connection = createPhysicalConnection ()
If you meet the condition errorCount > connectionErrorRetryAttempts & & timeBetweenConnectErrorMillis > 0, you will try to reconnect again. Take a look at the meaning of these parameters:
Number of errorCount errors
It is zero when the run method is initialized, and 1 is added automatically every time the connection fails.
ConnectionErrorRetryAttempts
The number of connection error retries. The default value is 1.
Protected int connectionErrorRetryAttempts = 1
TimeBetweenConnectErrorMillis
Connection interval in milliseconds. The default value is 500.
Protected volatile long timeBetweenConnectErrorMillis = DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS;public static final long DEFAULT_TIME_BETWEEN_CONNECT_ERROR_MILLIS = 500,
After we fail to connect to the database, either break is interrupted in it, including
If (breakAfterAcquireFailure) {break;}
Change break-after-acquire-failure to true, and configure it in the application.properties file as follows:
Spring.datasource.druid.break-after-acquire-failure=true
If you want to try to connect several times, you need to set connection-error-retry-attempts. Only when errorCount is greater than connectionErrorRetryAttempts will you enter the condition and break the loop. Configure the following in the application.properties file:
This is the end of spring.datasource.druid.connection-error-retry-attempts=3 's article on "how to keep reconnecting after a failed connection to the springboot druid database connection pool". Thank you for reading! I believe that everyone has a certain understanding of "springboot druid database connection pool connection failure has been reconnected how to solve" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.