Home | History | Annotate | Download | only in sql
      1 /*
      2  * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     23  * questions.
     24  */
     25 
     26 package javax.sql;
     27 
     28 import java.sql.Connection;
     29 import java.sql.SQLException;
     30 
     31 /**
     32  * An object that provides hooks for connection pool management.
     33  * A <code>PooledConnection</code> object
     34  * represents a physical connection to a data source.  The connection
     35  * can be recycled rather than being closed when an application is
     36  * finished with it, thus reducing the number of connections that
     37  * need to be made.
     38  * <P>
     39  * An application programmer does not use the <code>PooledConnection</code>
     40  * interface directly; rather, it is used by a middle tier infrastructure
     41  * that manages the pooling of connections.
     42  * <P>
     43  * When an application calls the method <code>DataSource.getConnection</code>,
     44  * it gets back a <code>Connection</code> object.  If connection pooling is
     45  * being done, that <code>Connection</code> object is actually a handle to
     46  * a <code>PooledConnection</code> object, which is a physical connection.
     47  * <P>
     48  * The connection pool manager, typically the application server, maintains
     49  * a pool of <code>PooledConnection</code> objects.  If there is a
     50  * <code>PooledConnection</code> object available in the pool, the
     51  * connection pool manager returns a <code>Connection</code> object that
     52  * is a handle to that physical connection.
     53  * If no <code>PooledConnection</code> object is available, the
     54  * connection pool manager calls the <code>ConnectionPoolDataSource</code>
     55  * method <code>getPoolConnection</code> to create a new physical connection.  The
     56  *  JDBC driver implementing <code>ConnectionPoolDataSource</code> creates a
     57  *  new <code>PooledConnection</code> object and returns a handle to it.
     58  * <P>
     59  * When an application closes a connection, it calls the <code>Connection</code>
     60  * method <code>close</code>. When connection pooling is being done,
     61  * the connection pool manager is notified because it has registered itself as
     62  * a <code>ConnectionEventListener</code> object using the
     63  * <code>ConnectionPool</code> method <code>addConnectionEventListener</code>.
     64  * The connection pool manager deactivates the handle to
     65  * the <code>PooledConnection</code> object and  returns the
     66  * <code>PooledConnection</code> object to the pool of connections so that
     67  * it can be used again.  Thus, when an application closes its connection,
     68  * the underlying physical connection is recycled rather than being closed.
     69  * <P>
     70  * The physical connection is not closed until the connection pool manager
     71  * calls the <code>PooledConnection</code> method <code>close</code>.
     72  * This method is generally called to have an orderly shutdown of the server or
     73  * if a fatal error has made the connection unusable.
     74  *
     75  * <p>
     76  * A connection pool manager is often also a statement pool manager, maintining
     77  *  a pool of <code>PreparedStatement</code> objects.
     78  *  When an application closes a prepared statement, it calls the
     79  *  <code>PreparedStatement</code>
     80  * method <code>close</code>. When <code>Statement</code> pooling is being done,
     81  * the pool manager is notified because it has registered itself as
     82  * a <code>StatementEventListener</code> object using the
     83  * <code>ConnectionPool</code> method <code>addStatementEventListener</code>.
     84  *  Thus, when an application closes its  <code>PreparedStatement</code>,
     85  * the underlying prepared statement is recycled rather than being closed.
     86  * <P>
     87  *
     88  * @since 1.4
     89  */
     90 
     91 public interface PooledConnection {
     92 
     93   /**
     94    * Creates and returns a <code>Connection</code> object that is a handle
     95    * for the physical connection that
     96    * this <code>PooledConnection</code> object represents.
     97    * The connection pool manager calls this method when an application has
     98    * called the method <code>DataSource.getConnection</code> and there are
     99    * no <code>PooledConnection</code> objects available. See the
    100    * {@link PooledConnection interface description} for more information.
    101    *
    102    * @return  a <code>Connection</code> object that is a handle to
    103    *          this <code>PooledConnection</code> object
    104    * @exception SQLException if a database access error occurs
    105    * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
    106    * this method
    107    * @since 1.4
    108    */
    109   Connection getConnection() throws SQLException;
    110 
    111   /**
    112    * Closes the physical connection that this <code>PooledConnection</code>
    113    * object represents.  An application never calls this method directly;
    114    * it is called by the connection pool module, or manager.
    115    * <P>
    116    * See the {@link PooledConnection interface description} for more
    117    * information.
    118    *
    119    * @exception SQLException if a database access error occurs
    120    * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
    121    * this method
    122    * @since 1.4
    123    */
    124   void close() throws SQLException;
    125 
    126   /**
    127    * Registers the given event listener so that it will be notified
    128    * when an event occurs on this <code>PooledConnection</code> object.
    129    *
    130    * @param listener a component, usually the connection pool manager,
    131    *        that has implemented the
    132    *        <code>ConnectionEventListener</code> interface and wants to be
    133    *        notified when the connection is closed or has an error
    134    * @see #removeConnectionEventListener
    135    */
    136   void addConnectionEventListener(ConnectionEventListener listener);
    137 
    138   /**
    139    * Removes the given event listener from the list of components that
    140    * will be notified when an event occurs on this
    141    * <code>PooledConnection</code> object.
    142    *
    143    * @param listener a component, usually the connection pool manager,
    144    *        that has implemented the
    145    *        <code>ConnectionEventListener</code> interface and
    146    *        been registered with this <code>PooledConnection</code> object as
    147    *        a listener
    148    * @see #addConnectionEventListener
    149    */
    150   void removeConnectionEventListener(ConnectionEventListener listener);
    151 
    152         /**
    153          * Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object.  Components that
    154          * wish to be notified when  <code>PreparedStatement</code>s created by the
    155          * connection are closed or are detected to be invalid may use this method
    156          * to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object.
    157          * <p>
    158          * @param listener      an component which implements the <code>StatementEventListener</code>
    159          *                                      interface that is to be registered with this <code>PooledConnection</code> object
    160          * <p>
    161          * @since 1.6
    162          */
    163         public void addStatementEventListener(StatementEventListener listener);
    164 
    165         /**
    166          * Removes the specified <code>StatementEventListener</code> from the list of
    167          * components that will be notified when the driver detects that a
    168          * <code>PreparedStatement</code> has been closed or is invalid.
    169          * <p>
    170          * @param listener      the component which implements the
    171          *                                      <code>StatementEventListener</code> interface that was previously
    172          *                                      registered with this <code>PooledConnection</code> object
    173          * <p>
    174          * @since 1.6
    175          */
    176         public void removeStatementEventListener(StatementEventListener listener);
    177 
    178  }
    179