鴻蒙OS PooledConnection

2022-11-02 10:53 更新

PooledConnection

  1. public interface PooledConnection

為連接池管理提供掛鉤的對(duì)象。 PooledConnection 對(duì)象表示與數(shù)據(jù)源的物理連接。 當(dāng)應(yīng)用程序完成連接時(shí),連接可以被回收而不是關(guān)閉,從而減少需要建立的連接數(shù)量。

應(yīng)用程序程序員不直接使用 PooledConnection 接口; 相反,它由管理連接池的中間層基礎(chǔ)設(shè)施使用。

當(dāng)應(yīng)用程序調(diào)用 DataSource.getConnection 方法時(shí),它會(huì)返回一個(gè) Connection 對(duì)象。 如果正在進(jìn)行連接池,則該 Connection 對(duì)象實(shí)際上是 PooledConnection 對(duì)象的句柄,它是一個(gè)物理連接。

連接池管理器(通常是應(yīng)用程序服務(wù)器)維護(hù)一個(gè) PooledConnection 對(duì)象池。如果池中有可用的 PooledConnection 對(duì)象,則連接池管理器會(huì)返回一個(gè) Connection 對(duì)象,該對(duì)象是該物理連接的句柄。如果沒(méi)有可用的 PooledConnection 對(duì)象,則連接池管理器調(diào)用 ConnectionPoolDataSource 方法 getPoolConnection 來(lái)創(chuàng)建新的物理連接。實(shí)現(xiàn) ConnectionPoolDataSource 的 JDBC 驅(qū)動(dòng)程序創(chuàng)建一個(gè)新的 PooledConnection 對(duì)象并返回一個(gè)句柄。

當(dāng)應(yīng)用程序關(guān)閉連接時(shí),它會(huì)調(diào)用 Connection 方法 close。完成連接池時(shí),會(huì)通知連接池管理器,因?yàn)樗咽褂?ConnectionPool 方法 addConnectionEventListener 將自己注冊(cè)為 ConnectionEventListener 對(duì)象。連接池管理器停用 PooledConnection 對(duì)象的句柄并將 PooledConnection 對(duì)象返回到連接池,以便可以再次使用它。因此,當(dāng)應(yīng)用程序關(guān)閉其連接時(shí),底層物理連接將被回收而不是被關(guān)閉。

在連接池管理器調(diào)用 PooledConnection 方法關(guān)閉之前,物理連接不會(huì)關(guān)閉。 通常調(diào)用此方法以有序關(guān)閉服務(wù)器,或者如果致命錯(cuò)誤導(dǎo)致連接不可用。

連接池管理器通常也是語(yǔ)句池管理器,維護(hù) PreparedStatement 對(duì)象池。 當(dāng)應(yīng)用程序關(guān)閉準(zhǔn)備好的語(yǔ)句時(shí),它會(huì)調(diào)用 PreparedStatement 方法 close。 當(dāng)語(yǔ)句池完成時(shí),池管理器會(huì)收到通知,因?yàn)樗咽褂?ConnectionPool 方法 addStatementEventListener 將自己注冊(cè)為 StatementEventListener 對(duì)象。 因此,當(dāng)應(yīng)用程序關(guān)閉其 PreparedStatement 時(shí),底層的準(zhǔn)備好的語(yǔ)句被回收而不是被關(guān)閉。

Since:

1.4

方法總結(jié)

修飾符和類型 方法 描述
void addConnectionEventListener(ConnectionEventListener listener) 注冊(cè)給定的事件偵聽(tīng)器,以便在此 PooledConnection 對(duì)象上發(fā)生事件時(shí)通知它。
void addStatementEventListener(StatementEventListener listener) 使用此 PooledConnection 對(duì)象注冊(cè) StatementEventListener。
void close() 關(guān)閉此 PooledConnection 對(duì)象表示的物理連接。
Connection getConnection() 創(chuàng)建并返回一個(gè) Connection 對(duì)象,該對(duì)象是此 PooledConnection 對(duì)象表示的物理連接的句柄。
void removeConnectionEventListener(ConnectionEventListener listener) 當(dāng)此 PooledConnection 對(duì)象上發(fā)生事件時(shí),從將通知的組件列表中刪除給定的事件偵聽(tīng)器。
void removeStatementEventListener(StatementEventListener listener) 當(dāng)驅(qū)動(dòng)程序檢測(cè)到 PreparedStatement 已關(guān)閉或無(wú)效時(shí),將通知的組件列表中刪除指定的 StatementEventListener。

方法詳情

getConnection

Connection getConnection() throws SQLException

創(chuàng)建并返回一個(gè) Connection 對(duì)象,該對(duì)象是此 PooledConnection 對(duì)象表示的物理連接的句柄。 當(dāng)應(yīng)用程序調(diào)用了 DataSource.getConnection 方法并且沒(méi)有可用的 PooledConnection 對(duì)象時(shí),連接池管理器調(diào)用此方法。 有關(guān)更多信息,請(qǐng)參閱 PooledConnection。

返回:

一個(gè) Connection 對(duì)象,它是此 PooledConnection 對(duì)象的句柄

Throws:

Throw名稱 Throw描述
SQLException 如果發(fā)生數(shù)據(jù)庫(kù)訪問(wèn)錯(cuò)誤
SQLFeatureNotSupportedException 如果 JDBC 驅(qū)動(dòng)程序不支持此方法

Since:

1.4

close

void close() throws SQLException

關(guān)閉此 PooledConnection 對(duì)象表示的物理連接。 應(yīng)用程序從不直接調(diào)用此方法; 它由連接池模塊或管理器調(diào)用。

Throws:

Throw名稱 Throw描述
SQLException 如果發(fā)生數(shù)據(jù)庫(kù)訪問(wèn)錯(cuò)誤
SQLFeatureNotSupportedException 如果 JDBC 驅(qū)動(dòng)程序不支持此方法

Since:

1.4

addConnectionEventListener

void addConnectionEventListener(ConnectionEventListener listener)

注冊(cè)給定的事件偵聽(tīng)器,以便在此 PooledConnection 對(duì)象上發(fā)生事件時(shí)通知它。

參數(shù):

參數(shù)名稱 參數(shù)描述
listener 一個(gè)組件,通常是連接池管理器,它實(shí)現(xiàn)了 ConnectionEventListener 接口,并希望在連接關(guān)閉或出現(xiàn)錯(cuò)誤時(shí)得到通知

removeConnectionEventListener

void removeConnectionEventListener(ConnectionEventListener listener)

當(dāng)此 PooledConnection 對(duì)象上發(fā)生事件時(shí),從將通知的組件列表中刪除給定的事件偵聽(tīng)器。

參數(shù):

參數(shù)名稱 參數(shù)描述
listener 一個(gè)組件,通常是連接池管理器,它已經(jīng)實(shí)現(xiàn)了 ConnectionEventListener 接口并作為偵聽(tīng)器注冊(cè)到此 PooledConnection 對(duì)象中

addStatementEventListener

void addStatementEventListener(StatementEventListener listener)

使用此 PooledConnection 對(duì)象注冊(cè) StatementEventListener。 希望在連接創(chuàng)建的 PreparedStatements 關(guān)閉或檢測(cè)到無(wú)效時(shí)收到通知的組件可以使用此方法向此 PooledConnection 對(duì)象注冊(cè) StatementEventListener。

參數(shù):

參數(shù)名稱 參數(shù)描述
listener 一個(gè)實(shí)現(xiàn) StatementEventListener 接口的組件,該接口將使用此 PooledConnection 對(duì)象注冊(cè)

Since:

1.6

removeStatementEventListener

void removeStatementEventListener(StatementEventListener listener)

當(dāng)驅(qū)動(dòng)程序檢測(cè)到 PreparedStatement 已關(guān)閉或無(wú)效時(shí),將通知的組件列表中刪除指定的 StatementEventListener。

參數(shù):

參數(shù)名稱 參數(shù)描述
listener 實(shí)現(xiàn)先前使用此 PooledConnection 對(duì)象注冊(cè)的 StatementEventListener 接口的組件

Since:

1.6

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)