current position:Home>Database connection pool Druid source code learning (V)

Database connection pool Druid source code learning (V)

2022-05-15 07:24:29InfoQ

1、 brief introduction

In this paper , We will understand  com.alibaba.druid.pool.DruidDataSource#getConnection()
 
Method execution flow .

2、 Environmental Science

os-window10
druid-1.2.8
jdk-1.8.0_312
maven-3.8.1

3、 Get the connection flow

1、 call com.alibaba.druid.pool.DruidDataSource#getConnection(long) Overloading methods
@Override
public DruidPooledConnection getConnection() throws SQLException {
 return getConnection(maxWait);
}
2、 perform init() Initialization method , If Filter If the collection is not empty, it will be executed using the responsibility chain mode Filter Of dataSource_getConnection Method .
public DruidPooledConnection getConnection(long maxWaitMillis) throws SQLException {
 init();

 if (filters.size() > 0) {
 FilterChainImpl filterChain = new FilterChainImpl(this);
 return filterChain.dataSource_connect(this, maxWaitMillis);
 } else {
 return getConnectionDirect(maxWaitMillis);
 }
}
3、 In the end, it's all execution com.alibaba.druid.pool.DruidDataSource#getConnectionDirect Method .getConnectionDirect The code logic is as follows :
3.1、 perform com.alibaba.druid.pool.DruidDataSource#getConnectionInternal Method
DruidPooledConnection poolableConnection;
try {
 poolableConnection = getConnectionInternal(maxWaitMillis);
} catch (GetConnectionTimeoutException ex) {
 if (notFullTimeoutRetryCnt <= this.notFullTimeoutRetryCount && !isFull()) {
 notFullTimeoutRetryCnt++;
 if (LOG.isWarnEnabled()) {
 LOG.warn(&quot;get connection timeout retry : &quot; + notFullTimeoutRetryCnt);
 }
 continue;
 }
 throw ex;
}
3.2、
testOnBorrow
Parameter is true Check if the connection is available , Discard the unavailable connections
if (testOnBorrow) {
boolean validate = testConnectionInternal(poolableConnection.holder, poolableConnection.conn);
if (!validate) {
if (LOG.isDebugEnabled()) {
LOG.debug(&quot;skip not validate connection.&quot;);
}

discardConnection(poolableConnection.holder);
continue;
}
3.3、
testOnBorrow
Parameter is false The execution process is as follows: :

3.3.1、 Check if the connection is closed
if (poolableConnection.conn.isClosed()) {
 discardConnection(poolableConnection.holder); //  Pass in null, Avoid closing repeatedly
 continue;
}
3.3.2、 Check if... Is configured testWhileIdle、removeAbandoned Parameters , And do corresponding processing according to the configuration .
if (testWhileIdle) {
 final DruidConnectionHolder holder = poolableConnection.holder;
 long currentTimeMillis = System.currentTimeMillis();
 long lastActiveTimeMillis = holder.lastActiveTimeMillis;
 long lastExecTimeMillis = holder.lastExecTimeMillis;
 long lastKeepTimeMillis = holder.lastKeepTimeMillis;

 if (checkExecuteTime
 && lastExecTimeMillis != lastActiveTimeMillis) {
 lastActiveTimeMillis = lastExecTimeMillis;
 }

 if (lastKeepTimeMillis > lastActiveTimeMillis) {
 lastActiveTimeMillis = lastKeepTimeMillis;
 }

 long idleMillis = currentTimeMillis - lastActiveTimeMillis;

 long timeBetweenEvictionRunsMillis = this.timeBetweenEvictionRunsMillis;

 if (timeBetweenEvictionRunsMillis <= 0) {
 timeBetweenEvictionRunsMillis = DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
 }

 if (idleMillis >= timeBetweenEvictionRunsMillis
 || idleMillis < 0 // unexcepted branch
 ) {
 boolean validate = testConnectionInternal(poolableConnection.holder, poolableConnection.conn);
 if (!validate) {
 if (LOG.isDebugEnabled()) {
 LOG.debug(&quot;skip not validate connection.&quot;);
 }

 discardConnection(poolableConnection.holder);
 continue;
 }
 }
}
}

if (removeAbandoned) {
 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
 poolableConnection.connectStackTrace = stackTrace;
 poolableConnection.setConnectedTimeNano();
 poolableConnection.traceEnable = true;

 activeConnectionLock.lock();
 try {
 activeConnections.put(poolableConnection, PRESENT);
 } finally {
 activeConnectionLock.unlock();
 }
}
3.3.3、 Check whether the default auto submit
if (!this.defaultAutoCommit) {
 poolableConnection.setAutoCommit(false);
}

copyright notice
author[InfoQ],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/135/202205142328056320.html

Random recommended