日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文從 Commons DBCP testOnBorrow 的作用機制著手,管中窺豹,從一點去分析數據庫連接池獲取的過程以及架構分層設計。

以下內容會按照每層的作用,貫穿分析整個調用流程。

1??框架層 commons-pool

The indication of whether objects will be validated before being borrowed from the pool.

If the object fAIls to validate, it will be dropped from the pool, and we will attempt to borrow another.

testOnBorrow 不是 dbcp 定義的,是 commons-pool 定義的。commons-pool 詳細的定義了資源池使用的一套規范和運行流程。

/**

* Borrow an object from the pool. get object from 資源池

* @see org.Apache.commons.pool2.impl.GenericObjectPool#borrowObject(long)

*/

public T borrowObject(final long borrowMaxWaitMillis) throws Exception {

PooledObject<T> p = null;

// if validation fails, the instance is destroyed and the next available instance is examined.

// This continues until either a valid instance is returned or there are no more idle instances available.

while (p == null) {

// If there is one or more idle instance available in the pool,

// then an idle instance will be selected based on the value of getLifo(), activated and returned.

p = idleObjects.pollFirst();

if (p != null) {

// 設置 testOnBorrow 就會進行可用性校驗

if (p != null && (getTestOnBorrow() || create && getTestOnCreate())) {

boolean validate = false;

Throwable validationThrowable = null;

try {

// 具體的校驗實現由實現類完成。

// see org.apache.commons.dbcp2.PoolableConnectionFactory

validate = factory.validateObject(p);

} catch (final Throwable t) {

PoolUtils.checkRethrow(t);

validationThrowable = t;

}

if (!validate) {

try {

// 如果校驗異常,會銷毀該資源。

// obj is not valid and should be dropped from the pool

destroy(p);

destroyedByBorrowValidationCount.incrementAndGet();

} catch (final Exception e) {

// Ignore - validation failure is more important

}

p = null;

}

}

}

}

return p.getObject();

}

2??應用層 commons-dbcp

dbcp 是特定于管理數據庫連接的資源池。

PoolableConnectionFactory is a PooledObjectFactory

PoolableConnection is a PooledObject

/**

* @see PoolableConnectionFactory#validateObject(PooledObject)

*/

@Override

public boolean validateObject(final PooledObject<PoolableConnection> p) {

try {

/**

* 檢測資源池對象的創建時間,是否超過生存時間

* 如果超過 maxConnLifetimeMillis, 不再委托數據庫連接進行校驗,直接廢棄改資源

* @see PoolableConnectionFactory#setMaxConnLifetimeMillis(long)

*/

validateLifetime(p);

// 委托數據庫連接進行自我校驗

validateConnection(p.getObject());

return true;

} catch (final Exception e) {

return false;

}

}

/**

* 數據庫連接層的校驗。具體到是否已關閉、是否與 server 連接可用

* @see Connection#isValid(int)

*/

public void validateConnection(final PoolableConnection conn) throws SQLException {

if(conn.isClosed()) {

throw new SQLException("validateConnection: connection closed");

}

conn.validate(_validationQuery, _validationQueryTimeout);

}

3??基礎層 MySQL-connector-JAVA

Returns true if the connection has not been closed and is still valid.

這個是 java.sql.Connection 定義的規范。具體實現根據對應數據庫的 driver 來完成。使用某種機制用來探測連接是否可用。

/**

* 調用 com.mysql.jdbc.MysqlIO, 發送ping 請求,檢測是否可用

* 對比 H2 數據庫,是通過獲取當前事務級別來檢測連接是否可以。但是忽略了 timeout 配置,畢竟是 demo 數據庫

分享到:
標簽:架構
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定