您好,登錄后才能下訂單哦!
這篇“Druid核心源碼分析DruidDataSource”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Druid核心源碼分析DruidDataSource”文章吧。
druid連接池支持的所有連接參數可在類com.alibaba.druid.pool.DruidDataSourceFactory
中查看。
配置讀取代碼:
public void configFromPropety(Properties properties) { //這方法太長,自己看源碼去吧,就是讀讀屬性。。。。 }
整體代碼比較簡單,就是把配置內容,讀取到dataSource。
首先是簡單的判斷,加鎖:
if (inited) { //已經被初始化好了,直接return return; } // bug fixed for dead lock, for issue #2980 DruidDriver.getInstance(); /**控制創建移除連接的鎖,并且通過條件去控制一個連接的生成消費**/ // public DruidAbstractDataSource(boolean lockFair){ // lock = new ReentrantLock(lockFair); // // notEmpty = lock.newCondition(); // empty = lock.newCondition(); // } final ReentrantLock lock = this.lock; try { lock.lockInterruptibly(); } catch (InterruptedException e) { throw new SQLException("interrupt", e); }
之后會更新一些JMX的監控指標:
//一些jmx監控指標 this.connectionIdSeedUpdater.addAndGet(this, delta); this.statementIdSeedUpdater.addAndGet(this, delta); this.resultSetIdSeedUpdater.addAndGet(this, delta); this.transactionIdSeedUpdater.addAndGet(this, delta);
druid的監控指標都是通過jmx實現的。
解析連接串:
if (this.jdbcUrl != null) { //解析連接串 this.jdbcUrl = this.jdbcUrl.trim(); initFromWrapDriverUrl(); }
initFromWrapDriverUrl
方法,除了從jdbc url中解析出連接和驅動信息,后面還把filters的名字,解析成了對應的filter類。
private void initFromWrapDriverUrl() throws SQLException { if (!jdbcUrl.startsWith(DruidDriver.DEFAULT_PREFIX)) { return; } DataSourceProxyConfig config = DruidDriver.parseConfig(jdbcUrl, null); this.driverClass = config.getRawDriverClassName(); LOG.error("error url : '" + jdbcUrl + "', it should be : '" + config.getRawUrl() + "'"); this.jdbcUrl = config.getRawUrl(); if (this.name == null) { this.name = config.getName(); } for (Filter filter : config.getFilters()) { addFilter(filter); } }
之后在init方法里面,會進行filters的初始化:
//初始化filter 屬性 for (Filter filter : filters) { filter.init(this); }
之后解析數據庫類型:
if (this.dbTypeName == null || this.dbTypeName.length() == 0) { this.dbTypeName = JdbcUtils.getDbType(jdbcUrl, null); }
注意枚舉值: com.alibaba.druid.DbType
,這個里面包含了目前durid連接池支持的所有數據源 類型,另外,druid還額外提供了一些驅動類,例如:
elastic_search (1 << 25), // com.alibaba.xdriver.elastic.jdbc.ElasticDriver
clickhouse還提供了負載均衡的驅動類:
com.alibaba.druid.support.clickhouse.BalancedClickhouseDriver
。
在回到init方法,之后是一堆參數解析,不再說,跳過了。 之后是通過SPI加載自定義的filter:
private void initFromSPIServiceLoader() { if (loadSpifilterSkip) { return; } if (autoFilters == null) { List<Filter> filters = new ArrayList<Filter>(); ServiceLoader<Filter> autoFilterLoader = ServiceLoader.load(Filter.class); for (Filter filter : autoFilterLoader) { AutoLoad autoLoad = filter.getClass().getAnnotation(AutoLoad.class); if (autoLoad != null && autoLoad.value()) { filters.add(filter); } } autoFilters = filters; } for (Filter filter : autoFilters) { if (LOG.isInfoEnabled()) { LOG.info("load filter from spi :" + filter.getClass().getName()); } addFilter(filter); } }
注意自定義的filter,要使用com.alibaba.druid.filter.AutoLoad
。
解析驅動:
protected void resolveDriver() throws SQLException { if (this.driver == null) { if (this.driverClass == null || this.driverClass.isEmpty()) { this.driverClass = JdbcUtils.getDriverClassName(this.jdbcUrl); } if (MockDriver.class.getName().equals(driverClass)) { driver = MockDriver.instance; } else if ("com.alibaba.druid.support.clickhouse.BalancedClickhouseDriver".equals(driverClass)) { Properties info = new Properties(); info.put("user", username); info.put("password", password); info.putAll(connectProperties); driver = new BalancedClickhouseDriver(jdbcUrl, info); } else { if (jdbcUrl == null && (driverClass == null || driverClass.length() == 0)) { throw new SQLException("url not set"); } driver = JdbcUtils.createDriver(driverClassLoader, driverClass); } } else { if (this.driverClass == null) { this.driverClass = driver.getClass().getName(); } } }
其中durid自己的mock驅動和clickhouse的負載均衡的驅動,特殊判斷了下,其他走的都是class forname.
之后是exception sorter和checker的一些東西,跟主線劇情關系不大,skip.
之后是一些初始化JdbcDataSourceStat
,沒啥東西。
之后是核心:
connections = new DruidConnectionHolder[maxActive]; //連接數組 evictConnections = new DruidConnectionHolder[maxActive]; //銷毀的連接數組 keepAliveConnections = new DruidConnectionHolder[maxActive]; //保持活躍可用的數組
dataSource的連接,都被包裝在類DruidConnectionHolder
中,之后是一個同步去初始化連接還是異步去初始化的連接,總之,是去初始化 連接的過程:
if (createScheduler != null && asyncInit) { for (int i = 0; i < initialSize; ++i) { submitCreateTask(true); } } else if (!asyncInit) { // init connections while (poolingCount < initialSize) { try { PhysicalConnectionInfo pyConnectInfo = createPhysicalConnection(); DruidConnectionHolder holder = new DruidConnectionHolder(this, pyConnectInfo); connections[poolingCount++] = holder; } catch (SQLException ex) { LOG.error("init datasource error, url: " + this.getUrl(), ex); if (initExceptionThrow) { connectError = ex; break; } else { Thread.sleep(3000); } } } if (poolingCount > 0) { poolingPeak = poolingCount; poolingPeakTime = System.currentTimeMillis(); } }
初始化的連接個數為連接串里面配置的initialSize
.
核心初始化方法com.alibaba.druid.pool.DruidAbstractDataSource#createPhysicalConnection()
,在這方法里面,會拿用戶名密碼,之后執行真正的獲取connection:
public Connection createPhysicalConnection(String url, Properties info) throws SQLException { Connection conn; if (getProxyFilters().size() == 0) { conn = getDriver().connect(url, info); } else { conn = new FilterChainImpl(this).connection_connect(info); } createCountUpdater.incrementAndGet(this); return conn; }
注意,如果配置了filters,則所有操作,都會在操作前執行filter處理鏈。
public ConnectionProxy connection_connect(Properties info) throws SQLException { if (this.pos < filterSize) { return nextFilter() .connection_connect(this, info); } Driver driver = dataSource.getRawDriver(); String url = dataSource.getRawJdbcUrl(); Connection nativeConnection = driver.connect(url, info); if (nativeConnection == null) { return null; } return new ConnectionProxyImpl(dataSource, nativeConnection, info, dataSource.createConnectionId()); }
再回到主流程init方法,connections
數組初始化完成之后, 開啟額外線程:
createAndLogThread(); //打印連接信息 createAndStartCreatorThread(); //創建連接線程 createAndStartDestroyThread(); //銷毀連接線程
先看注釋,具體里面的內容后面單獨拉出來講。
之后:
initedLatch.await(); //初始化 latch -1 init = true; //標記已經初始化完成 initedTime = new Date(); //時間 registerMbean(); //為datasource 注冊jmx監控指標
最后的最后,如果配置了keepAlive:
if (keepAlive) { // async fill to minIdle if (createScheduler != null) { for (int i = 0; i < minIdle; ++i) { submitCreateTask(true); } } else { this.emptySignal(); } }
這時候,會根據配置的活躍連接數minIdle
,去給datasource的連接,做個保持活躍連接個數,具體后面再說。
首先,使用數組作為連接的容器,對于真實連接的加入和移除,使用lock就行同步,另外,在加入和移除連接時候,對比生產消費模型,通過lock上的條件,來通知是否可以獲取或者加入連接。
public DruidAbstractDataSource(boolean lockFair){ lock = new ReentrantLock(lockFair); notEmpty = lock.newCondition(); //非空,有連接 empty = lock.newCondition(); //空的 }
另外,默認的fairlock為false
public DruidDataSource(){ this(false); } public DruidDataSource(boolean fairLock){ super(fairLock); configFromPropety(System.getProperties()); }
在線程com.alibaba.druid.pool.DruidDataSource.CreateConnectionThread
中:
if (emptyWait) { // 必須存在線程等待,才創建連接 if (poolingCount >= notEmptyWaitThreadCount // && (!(keepAlive && activeCount + poolingCount < minIdle)) && !isFailContinuous() ) { empty.await(); } // 防止創建超過maxActive數量的連接 if (activeCount + poolingCount >= maxActive) { empty.await(); continue; } }
必須存在線程等待獲取連接時候,才能創建連接,并且要保持總的連接數,不能超過配置的最大連接。
創建完連接之后,執行 notEmpty.signalAll();
通知消費者。
外層代碼:
@Override public DruidPooledConnection getConnection() throws SQLException { return getConnection(maxWait); } 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); } }
忽略掉filter chain,其實最后執行的還是com.alibaba.druid.pool.DruidDataSource#getConnectionDirect
:
方法內部:
poolableConnection = getConnectionInternal(maxWaitMillis);
1 , 連接不足,需要直接去創建新的,跟我們初始化一樣
2,從connections里面拿
if (maxWait > 0) { holder = pollLast(nanos); } else { holder = takeLast(); }
其中,maxWait默認為-1,配置在init里面:
String property = properties.getProperty("druid.maxWait"); if (property != null && property.length() > 0) { try { int value = Integer.parseInt(property); this.setMaxWait(value); } catch (NumberFormatException e) { LOG.error("illegal property 'druid.maxWait'", e); } }
這個用于配置拿連接時候,是否在這個時間上進行等待,默認是否,即一直等到拿到連接為止。
直接看下阻塞拿的過程:
DruidConnectionHolder takeLast() throws InterruptedException, SQLException { try { //沒連接了 while (poolingCount == 0) { //暗示下創建線程沒連接了 emptySignal(); // send signal to CreateThread create connection if (failFast && isFailContinuous()) { throw new DataSourceNotAvailableException(createError); } notEmptyWaitThreadCount++; if (notEmptyWaitThreadCount > notEmptyWaitThreadPeak) { notEmptyWaitThreadPeak = notEmptyWaitThreadCount; } try { //傻等著創建或者回收,能給整出來點兒連接 notEmpty.await(); // signal by recycle or creator } finally { notEmptyWaitThreadCount--; } notEmptyWaitCount++; if (!enable) { connectErrorCountUpdater.incrementAndGet(this); if (disableException != null) { throw disableException; } throw new DataSourceDisableException(); } } } catch (InterruptedException ie) { notEmpty.signal(); // propagate to non-interrupted thread notEmptySignalCount++; throw ie; } //拿數組的最后一個連接 decrementPoolingCount(); DruidConnectionHolder last = connections[poolingCount]; connections[poolingCount] = null; return last; }
protected void createAndStartDestroyThread() { destroyTask = new DestroyTask(); //自定義配置銷毀 ,適用于連接數非常多的 情況 if (destroyScheduler != null) { long period = timeBetweenEvictionRunsMillis; if (period <= 0) { period = 1000; } destroySchedulerFuture = destroyScheduler.scheduleAtFixedRate(destroyTask, period, period, TimeUnit.MILLISECONDS); initedLatch.countDown(); return; } String threadName = "Druid-ConnectionPool-Destroy-" + System.identityHashCode(this); //單線程銷毀 destroyConnectionThread = new DestroyConnectionThread(threadName); destroyConnectionThread.start(); }
實際的銷毀:
public class DestroyTask implements Runnable { public DestroyTask() { } @Override public void run() { shrink(true, keepAlive); if (isRemoveAbandoned()) { removeAbandoned(); } } }
最終 執行的還是 shrink
方法。
public void shrink(boolean checkTime, boolean keepAlive) { try { lock.lockInterruptibly(); } catch (InterruptedException e) { return; } boolean needFill = false; int evictCount = 0; int keepAliveCount = 0; int fatalErrorIncrement = fatalErrorCount - fatalErrorCountLastShrink; fatalErrorCountLastShrink = fatalErrorCount; try { if (!inited) { return; } final int checkCount = poolingCount - minIdle; //需要檢測連接的數量 final long currentTimeMillis = System.currentTimeMillis(); for (int i = 0; i < poolingCount; ++i) { //檢測目前connections數組中的連接 DruidConnectionHolder connection = connections[i]; if ((onFatalError || fatalErrorIncrement > 0) && (lastFatalErrorTimeMillis > connection.connectTimeMillis)) { keepAliveConnections[keepAliveCount++] = connection; continue; } if (checkTime) { //是否設置了物理連接的超時時間phyTimoutMills。假如設置了該時間, // 判斷連接時間存活時間是否已經超過phyTimeoutMills,是則放入evictConnections中 if (phyTimeoutMillis > 0) { long phyConnectTimeMillis = currentTimeMillis - connection.connectTimeMillis; if (phyConnectTimeMillis > phyTimeoutMillis) { evictConnections[evictCount++] = connection; continue; } } long idleMillis = currentTimeMillis - connection.lastActiveTimeMillis;//獲取連接空閑時間 //如果某條連接空閑時間小于minEvictableIdleTimeMillis,則不用繼續檢查剩下的連接了 if (idleMillis < minEvictableIdleTimeMillis && idleMillis < keepAliveBetweenTimeMillis ) { break; } if (idleMillis >= minEvictableIdleTimeMillis) { // check checkTime is silly code //檢測檢查了幾個連接了 if (checkTime && i < checkCount) { //超時了 evictConnections[evictCount++] = connection; continue; } else if (idleMillis > maxEvictableIdleTimeMillis) { //超時了 evictConnections[evictCount++] = connection; continue; } } if (keepAlive && idleMillis >= keepAliveBetweenTimeMillis) { //配置了keepAlive,并且在存活時間內,放到keepAlive數組 keepAliveConnections[keepAliveCount++] = connection; } } else { //不需要檢查時間的,直接移除 if (i < checkCount) { evictConnections[evictCount++] = connection; } else { break; } } } int removeCount = evictCount + keepAliveCount; //移除了幾個 //由于使用connections連接時候,都是取后面的,后面 的是最新的連接,只考慮前面過期就行,所以只需要挪動前面的連接 if (removeCount > 0) { System.arraycopy(connections, removeCount, connections, 0, poolingCount - removeCount); Arrays.fill(connections, poolingCount - removeCount, poolingCount, null); poolingCount -= removeCount; } keepAliveCheckCount += keepAliveCount; if (keepAlive && poolingCount + activeCount < minIdle) { //不夠核心的活躍連接時候,需要去創建啦 needFill = true; } } finally { lock.unlock(); } if (evictCount > 0) { for (int i = 0; i < evictCount; ++i) { //銷毀連接 DruidConnectionHolder item = evictConnections[i]; Connection connection = item.getConnection(); JdbcUtils.close(connection); destroyCountUpdater.incrementAndGet(this); } Arrays.fill(evictConnections, null); } if (keepAliveCount > 0) { // keep order for (int i = keepAliveCount - 1; i >= 0; --i) { DruidConnectionHolder holer = keepAliveConnections[i]; Connection connection = holer.getConnection(); holer.incrementKeepAliveCheckCount(); boolean validate = false; try { this.validateConnection(connection); validate = true; } catch (Throwable error) { if (LOG.isDebugEnabled()) { LOG.debug("keepAliveErr", error); } // skip } boolean discard = !validate; //沒通過validate if (validate) { //通過keep alive檢查,更新時間 holer.lastKeepTimeMillis = System.currentTimeMillis(); //這里還會嘗試放回connections數組 boolean putOk = put(holer, 0L, true); if (!putOk) { //沒放入,標記要丟棄了 discard = true; } } if (discard) { try { connection.close(); } catch (Exception e) { // skip } lock.lock(); try { discardCount++; if (activeCount + poolingCount <= minIdle) { //發信號讓創建線程去創建 emptySignal(); } } finally { lock.unlock(); } } } this.getDataSourceStat().addKeepAliveCheckCount(keepAliveCount); Arrays.fill(keepAliveConnections, null); } if (needFill) { //又要去創建了 lock.lock(); try { int fillCount = minIdle - (activeCount + poolingCount + createTaskCount); for (int i = 0; i < fillCount; ++i) { emptySignal(); } } finally { lock.unlock(); } } else if (onFatalError || fatalErrorIncrement > 0) { lock.lock(); try { emptySignal(); } finally { lock.unlock(); } } }
工具數組evictConnections
,keepAliveConnections
用完即被置空,老工具人了。
一波操作下來,完成了對connections數組的大清洗。
以上就是關于“Druid核心源碼分析DruidDataSource”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。