鸿蒙OS开发文档 鸿蒙OS PooledConnection

2024-02-25 开发教程 鸿蒙OS开发文档 匿名 2

PooledConnection

public interface PooledConnection

为连接池管理提供挂钩的对象。 PooledConnection 对象表示与数据源的物理连接。 当应用程序完成连接时,连接可以被回收而不是关闭,从而减少需要建立的连接数量。

应用程序程序员不直接使用 PooledConnection 接口; 相反,它由管理连接池的中间层基础设施使用。

当应用程序调用 DataSource.getConnection 方法时,它会返回一个 Connection 对象。 如果正在进行连接池,则该 Connection 对象实际上是 PooledConnection 对象的句柄,它是一个物理连接。

连接池管理器(通常是应用程序服务器)维护一个 PooledConnection 对象池。如果池中有可用的 PooledConnection 对象,则连接池管理器会返回一个 Connection 对象,该对象是该物理连接的句柄。如果没有可用的 PooledConnection 对象,则连接池管理器调用 ConnectionPoolDataSource 方法 getPoolConnection 来创建新的物理连接。实现 ConnectionPoolDataSource 的 JDBC 驱动程序创建一个新的 PooledConnection 对象并返回一个句柄。

当应用程序关闭连接时,它会调用 Connection 方法 close。完成连接池时,会通知连接池管理器,因为它已使用 ConnectionPool 方法 addConnectionEventListener 将自己注册为 ConnectionEventListener 对象。连接池管理器停用 PooledConnection 对象的句柄并将 PooledConnection 对象返回到连接池,以便可以再次使用它。因此,当应用程序关闭其连接时,底层物理连接将被回收而不是被关闭。

在连接池管理器调用 PooledConnection 方法关闭之前,物理连接不会关闭。 通常调用此方法以有序关闭服务器,或者如果致命错误导致连接不可用。

连接池管理器通常也是语句池管理器,维护 PreparedStatement 对象池。 当应用程序关闭准备好的语句时,它会调用 PreparedStatement 方法 close。 当语句池完成时,池管理器会收到通知,因为它已使用 ConnectionPool 方法 addStatementEventListener 将自己注册为 StatementEventListener 对象。 因此,当应用程序关闭其 PreparedStatement 时,底层的准备好的语句被回收而不是被关闭。

Since:

1.4

方法总结

修饰符和类型方法描述
voidaddConnectionEventListener(ConnectionEventListener listener)注册给定的事件侦听器,以便在此 PooledConnection 对象上发生事件时通知它。
voidaddStatementEventListener(StatementEventListener listener)使用此 PooledConnection 对象注册 StatementEventListener。
voidclose()关闭此 PooledConnection 对象表示的物理连接。
ConnectiongetConnection()创建并返回一个 Connection 对象,该对象是此 PooledConnection 对象表示的物理连接的句柄。
voidremoveConnectionEventListener(ConnectionEventListener listener)当此 PooledConnection 对象上发生事件时,从将通知的组件列表中删除给定的事件侦听器。
voidremoveStatementEventListener(StatementEventListener listener)当驱动程序检测到 PreparedStatement 已关闭或无效时,将通知的组件列表中删除指定的 StatementEventListener。

方法详情

getConnection

Connection getConnection() throws SQLException

创建并返回一个 Connection 对象,该对象是此 PooledConnection 对象表示的物理连接的句柄。 当应用程序调用了 DataSource.getConnection 方法并且没有可用的 PooledConnection 对象时,连接池管理器调用此方法。 有关更多信息,请参阅 PooledConnection。

返回:

一个 Connection 对象,它是此 PooledConnection 对象的句柄

Throws:

Throw名称Throw描述
SQLException如果发生数据库访问错误
SQLFeatureNotSupportedException如果 JDBC 驱动程序不支持此方法

Since:

1.4

close

void close() throws SQLException

关闭此 PooledConnection 对象表示的物理连接。 应用程序从不直接调用此方法; 它由连接池模块或管理器调用。

Throws:

Throw名称Throw描述
SQLException如果发生数据库访问错误
SQLFeatureNotSupportedException如果 JDBC 驱动程序不支持此方法

Since:

1.4

addConnectionEventListener

void addConnectionEventListener(ConnectionEventListener listener)

注册给定的事件侦听器,以便在此 PooledConnection 对象上发生事件时通知它。

参数:

参数名称参数描述
listener一个组件,通常是连接池管理器,它实现了 ConnectionEventListener 接口,并希望在连接关闭或出现错误时得到通知

removeConnectionEventListener

void removeConnectionEventListener(ConnectionEventListener listener)

当此 PooledConnection 对象上发生事件时,从将通知的组件列表中删除给定的事件侦听器。

参数:

参数名称参数描述
listener一个组件,通常是连接池管理器,它已经实现了 ConnectionEventListener 接口并作为侦听器注册到此 PooledConnection 对象中

addStatementEventListener

void addStatementEventListener(StatementEventListener listener)

使用此 PooledConnection 对象注册 StatementEventListener。 希望在连接创建的 PreparedStatements 关闭或检测到无效时收到通知的组件可以使用此方法向此 PooledConnection 对象注册 StatementEventListener。

参数:

参数名称参数描述
listener一个实现 StatementEventListener 接口的组件,该接口将使用此 PooledConnection 对象注册

Since:

1.6

removeStatementEventListener

void removeStatementEventListener(StatementEventListener listener)

当驱动程序检测到 PreparedStatement 已关闭或无效时,将通知的组件列表中删除指定的 StatementEventListener。

参数:

参数名称参数描述
listener实现先前使用此 PooledConnection 对象注册的 StatementEventListener 接口的组件

Since:

1.6