Most visited

Recently visited

Added in API level 1

PooledConnection

public interface PooledConnection

javax.sql.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方法close之前,物理连接并未关闭。 此方法通常被称为有序关闭服务器,或者如果致命错误导致连接不可用。

连接池管理器通常也是一个语句池管理器,保持一个PreparedStatement对象池。 当应用程序关闭准备好的语句时,它会调用PreparedStatement方法close 。 当Statement池正在完成时,池管理器将被通知,因为它已使用ConnectionPool方法addStatementEventListener将自己注册为StatementEventListener对象。 因此,当应用程序关闭其PreparedStatement ,基础准备好的语句将被循环使用,而不是被关闭。

Summary

Public methods

abstract void addConnectionEventListener(ConnectionEventListener listener)

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

abstract void addStatementEventListener(StatementEventListener listener)

用这个 PooledConnection对象注册一个 StatementEventListener 。

abstract void close()

关闭此 PooledConnection对象所代表的物理连接。

abstract Connection getConnection()

创建并返回一个 Connection对象,它是对于这个物理连接的手柄 PooledConnection对象表示。

abstract void removeConnectionEventListener(ConnectionEventListener listener)

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

abstract void removeStatementEventListener(StatementEventListener listener)

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

Public methods

addConnectionEventListener

Added in API level 1
void addConnectionEventListener (ConnectionEventListener listener)

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

Parameters
listener ConnectionEventListener: a component, usually the connection pool manager, that has implemented the ConnectionEventListener interface and wants to be notified when the connection is closed or has an error

也可以看看:

addStatementEventListener

Added in API level 9
void addStatementEventListener (StatementEventListener listener)

用这个PooledConnection对象注册一个StatementEventListener 。 当要被通知希望组分PreparedStatement由连接创建s的关闭或被检测为无效可使用此方法来注册StatementEventListener与此PooledConnection对象。

Parameters
listener StatementEventListener: an component which implements the StatementEventListener interface that is to be registered with this PooledConnection object

close

Added in API level 1
void close ()

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

有关更多信息,请参阅 interface description 。

Throws
SQLException if a database access error occurs
if the JDBC driver does not support this method

getConnection

Added in API level 1
Connection getConnection ()

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

Returns
Connection a Connection object that is a handle to this PooledConnection object
Throws
SQLException if a database access error occurs
if the JDBC driver does not support this method

removeConnectionEventListener

Added in API level 1
void removeConnectionEventListener (ConnectionEventListener listener)

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

Parameters
listener ConnectionEventListener: a component, usually the connection pool manager, that has implemented the ConnectionEventListener interface and been registered with this PooledConnection object as a listener

也可以看看:

removeStatementEventListener

Added in API level 9
void removeStatementEventListener (StatementEventListener listener)

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

Parameters
listener StatementEventListener: the component which implements the StatementEventListener interface that was previously registered with this PooledConnection object

Hooray!