模块  java.sql
软件包  javax.sql

Interface PooledConnection

  • All Known Subinterfaces:
    XAConnection

    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对象,以便它可以再次使用连接池。 因此,当应用程序关闭其连接时,底层物理连接将被回收而不是被关闭。

    如果连接池管理器包装或提供对从PoolConnection.getConnection调用返回的逻辑句柄的代理,则池管理器必须在连接池管理器关闭时执行以下操作之一,或者将PooledConnection返回到池以响应应用程序调用Connection.close

    • 在逻辑Connection句柄上调用endRequest
    • 在逻辑Connection句柄上调用close

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

    连接池管理器通常也是语句池管理器,维护一个PreparedStatement对象池。 当应用程序关闭PreparedStatement准备语句时,它会调用PreparedStatement方法close Statement池化Statement ,将通知池管理器,因为它已使用ConnectionPool方法addStatementEventListener将自身注册为StatementEventListener对象。 因此,当应用程序关闭其PreparedStatement ,基础PreparedStatement准备语句将被回收而不是被关闭。

    从以下版本开始:
    1.4
    • 方法详细信息

      • getConnection

        Connection getConnection()
                          throws SQLException
        创建并返回一个Connection对象,该对象是此PooledConnection对象表示的物理连接的句柄。 当应用程序调用方法DataSource.getConnection并且没有可用的PooledConnection对象时,连接池管理器将调用此方法。 有关更多信息,请参见interface description
        结果
        Connection对象,它是此 PooledConnection对象的句柄
        异常
        SQLException - 如果发生数据库访问错误
        SQLFeatureNotSupportedException - 如果JDBC驱动程序不支持此方法
        从以下版本开始:
        1.4
      • close

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

        有关更多信息,请参见interface description

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

        void removeConnectionEventListener​(ConnectionEventListener listener)
        从此 PooledConnection对象上发生事件时将通知的组件列表中删除给定的事件侦听器。
        参数
        listener - 一个组件,通常是连接池管理器,已实现 ConnectionEventListener接口并已在此 PooledConnection对象中注册为侦听器
        另请参见:
        addConnectionEventListener(javax.sql.ConnectionEventListener)
      • addStatementEventListener

        void addStatementEventListener​(StatementEventListener listener)
        注册一个StatementEventListener这个PooledConnection对象。 当要被通知希望组分PreparedStatement由连接创建s的关闭或被检测为无效可使用此方法来注册StatementEventListener与此PooledConnection对象。
        参数
        listener - 实现 StatementEventListener接口的组件,该接口将向此 PooledConnection对象注册
        从以下版本开始:
        1.6
      • removeStatementEventListener

        void removeStatementEventListener​(StatementEventListener listener)
        从驱动程序检测到 PreparedStatement已关闭或无效时将通知的组件列表中删除指定的 StatementEventListener
        参数
        listener - 实现先前在此 PooledConnection对象中注册的 StatementEventListener接口的组件
        从以下版本开始:
        1.6