dbcp原始碼分析 獲取連線物件

2021-10-10 23:07:40 字數 2786 閱讀 9973

/**

* create (if necessary) and return a connection to the database.

* * @throws sqlexception if a database access error occurs

* @return a database connection

*/public connection getconnection() throws sqlexception

核心步驟

1.從連線池獲取連線物件

2.校驗連線是否有效

原始碼genericobjectpool

public t borrowobject() throws exception 

//從連線池獲取連線

this.allocate();

while(true)

if (latch.getpair() == null && !latch.maycreate())

break;

}case 1:

try

if (maxwait <= 0l) else }}

if (this.isclosed())

} catch (interruptedexception var51) else if (latch.getpair() == null && latch.maycreate()) else

}if (doallocate)

thread.currentthread().interrupt();

throw var51;

}if (maxwait <= 0l || system.currenttimemillis() - starttime < maxwait)

synchronized(this)

this._allocationqueue.remove(latch);

}throw new nosuchelementexception("timeout waiting for idle object");

case 2:

synchronized(this)

break;

}default:

throw new illegalargumentexception("whenexhaustedaction property " + whenexhaustedaction + " not recognized.");}}

boolean newlycreated = false;

if (null == latch.getpair()) finally

this.allocate();}}

}if (!newlycreated)

this.allocate();}}

//校驗連線是否有效

try

synchronized(this)

//返回連線物件

return latch.getpair().value; //返回連線物件

} catch (throwable var47) catch (throwable var42)

synchronized(this)

}this.allocate();

if (newlycreated) }}}

真正獲取連線物件的是這個方法

private cursorablelinkedlist> _pool; //連線池,是乙個linkedlist

//從連線池獲取連線

private synchronized void allocate()

}while(!this._allocationqueue.isempty() && (this._maxactive < 0 || this._numactive + this._numinternalprocessing < this._maxactive)) }}}

public boolean validateobject(object obj)  catch (exception var3) 

} else

}

public void validateconnection(connection conn) throws sqlexception  else 

rset = stmt.executequery(query);

if (!rset.next())

} finally catch (exception var14)

}if (stmt != null) catch (exception var13) }}

}}}

封裝了連線物件

private static final class latch

}while(!this._allocationqueue.isempty() && (this._maxactive < 0 || this._numactive + this._numinternalprocessing < this._maxactive)) }}}

1、獲取連線物件

真正獲取連線物件的方法

2、校驗連線是否有效

dbcp是基於pool,底層的連線池都是pool實現的。

go redis 連線池原始碼分析

1 建立連線和關閉連線 2 池子裡面取conn的管理 3 監控統計 4 整個pooler池子的關閉 結構體type connpool struct 池子裡面空閒的conn的同步channel connsmu sync.mutex conns conn 活躍的active conns idleconn...

spring原始碼分析 spring原始碼分析

1.spring 執行原理 spring 啟動時讀取應用程式提供的 bean 配置資訊,並在 spring 容器中生成乙份相應的 bean 配置登錄檔,然後根據這張登錄檔例項化 bean,裝配好 bean 之間的依賴關係,為上 層應用提供準備就緒的執行環境。二 spring 原始碼分析 1.1spr...

獲取Android原始碼

git是linux torvalds為了幫助管理linux核心開發而開發的乙個開放原始碼的公布式版本控制軟體。在git版本控制系統中,每乙個工作目錄都包含乙個完整倉庫,它們支援離線工作。android是由kernel dalvik bionic build等多個git專案組成的,所以android專...