JavaTM 2 Platform
Standard Ed. 6

javax.sql
接口 PooledConnection

所有已知子接口:
XAConnection

public interface PooledConnection

为连接池管理提供钩子 (hook) 的对象。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 时,底层准备好的语句会被回收而不是被关闭。

从以下版本开始:
1.4

方法摘要
 void addConnectionEventListener(ConnectionEventListener listener)
          注册给定的事件侦听器,以便在此 PooledConnection 对象上发生事件时它将得到通知。
 void addStatementEventListener(StatementEventListener listener)
          向此 PooledConnection 对象注册一个 StatementEventListener
 void close()
          关闭此 PooledConnection 对象表示的物理连接。
 Connection getConnection()
          创建并返回一个 Connection 对象,它是此 PooledConnection 对象表示的物理连接的句柄。
 void removeConnectionEventListener(ConnectionEventListener listener)
          从在此 PooledConnection 对象上发生事件时得到通知的组件所组成的列表中移除给定的事件侦听器。
 void removeStatementEventListener(StatementEventListener listener)
          从一个组件列表中移除指定的 StatementEventListener,该列表由驱动程序检测到 PreparedStatement 已关闭或无效时将获得通知的组件组成。
 

方法详细信息

getConnection

Connection getConnection()
                         throws SQLException
创建并返回一个 Connection 对象,它是此 PooledConnection 对象表示的物理连接的句柄。当应用程序已经调用 DataSource.getConnection 方法但不存在可用的 PooledConnection 对象时,连接池管理器将调用此方法。有关更多信息,请参阅接口描述

返回:
一个 Connection 对象,它是到此 PooledConnection 对象的句柄
抛出:
SQLException - 如果发生数据库访问错误
SQLFeatureNotSupportedException - 如果 JDBC 驱动程序不支持此方法
从以下版本开始:
1.4

close

void close()
           throws SQLException
关闭此 PooledConnection 对象表示的物理连接。应用程序永远不会直接调用此方法;它由连接池模块或管理器调用。

有关更多信息,请参阅接口描述

抛出:
SQLException - 如果发生数据库访问错误
SQLFeatureNotSupportedException - 如果 JDBC 驱动程序不支持此方法
从以下版本开始:
1.4

addConnectionEventListener

void addConnectionEventListener(ConnectionEventListener listener)
注册给定的事件侦听器,以便在此 PooledConnection 对象上发生事件时它将得到通知。

参数:
listener - 一个组件(通常为连接池管理器),它实现了 ConnectionEventListener 接口,并在关闭连接或发生错误时应得到通知
另请参见:
removeConnectionEventListener(javax.sql.ConnectionEventListener)

removeConnectionEventListener

void removeConnectionEventListener(ConnectionEventListener listener)
从在此 PooledConnection 对象上发生事件时得到通知的组件所组成的列表中移除给定的事件侦听器。

参数:
listener - 一个组件(通常为连接池管理器),它实现了 ConnectionEventListener 接口,并已作为侦听器向此 PooledConnection 对象注册
另请参见:
addConnectionEventListener(javax.sql.ConnectionEventListener)

addStatementEventListener

void addStatementEventListener(StatementEventListener listener)
向此 PooledConnection 对象注册一个 StatementEventListener。该连接创建的 PreparedStatement 关闭或检测为无效时希望获得通知的组件可以使用此方法向此 PooledConnection 对象注册一个 StatementEventListener

参数:
listener - 实现将向此 PooledConnection 对象注册的 StatementEventListener 接口的组件

从以下版本开始:
1.6

removeStatementEventListener

void removeStatementEventListener(StatementEventListener listener)
从一个组件列表中移除指定的 StatementEventListener,该列表由驱动程序检测到 PreparedStatement 已关闭或无效时将获得通知的组件组成。

参数:
listener - 实现以前向此 PooledConnection 对象注册的 StatementEventListener 接口的组件

从以下版本开始:
1.6

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only