1 /* 2 * Copyright (c) 2000, 2013, 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, maintaining 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 * 87 * @since 1.4 88 */ 89 90 public interface PooledConnection { 91 92 /** 93 * Creates and returns a <code>Connection</code> object that is a handle 94 * for the physical connection that 95 * this <code>PooledConnection</code> object represents. 96 * The connection pool manager calls this method when an application has 97 * called the method <code>DataSource.getConnection</code> and there are 98 * no <code>PooledConnection</code> objects available. See the 99 * {@link PooledConnection interface description} for more information. 100 * 101 * @return a <code>Connection</code> object that is a handle to 102 * this <code>PooledConnection</code> object 103 * @exception SQLException if a database access error occurs 104 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support 105 * this method 106 * @since 1.4 107 */ 108 Connection getConnection() throws SQLException; 109 110 /** 111 * Closes the physical connection that this <code>PooledConnection</code> 112 * object represents. An application never calls this method directly; 113 * it is called by the connection pool module, or manager. 114 * <P> 115 * See the {@link PooledConnection interface description} for more 116 * information. 117 * 118 * @exception SQLException if a database access error occurs 119 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support 120 * this method 121 * @since 1.4 122 */ 123 void close() throws SQLException; 124 125 /** 126 * Registers the given event listener so that it will be notified 127 * when an event occurs on this <code>PooledConnection</code> object. 128 * 129 * @param listener a component, usually the connection pool manager, 130 * that has implemented the 131 * <code>ConnectionEventListener</code> interface and wants to be 132 * notified when the connection is closed or has an error 133 * @see #removeConnectionEventListener 134 */ 135 void addConnectionEventListener(ConnectionEventListener listener); 136 137 /** 138 * Removes the given event listener from the list of components that 139 * will be notified when an event occurs on this 140 * <code>PooledConnection</code> object. 141 * 142 * @param listener a component, usually the connection pool manager, 143 * that has implemented the 144 * <code>ConnectionEventListener</code> interface and 145 * been registered with this <code>PooledConnection</code> object as 146 * a listener 147 * @see #addConnectionEventListener 148 */ 149 void removeConnectionEventListener(ConnectionEventListener listener); 150 151 /** 152 * Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. Components that 153 * wish to be notified when <code>PreparedStatement</code>s created by the 154 * connection are closed or are detected to be invalid may use this method 155 * to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. 156 * 157 * @param listener an component which implements the <code>StatementEventListener</code> 158 * interface that is to be registered with this <code>PooledConnection</code> object 159 * 160 * @since 1.6 161 */ 162 public void addStatementEventListener(StatementEventListener listener); 163 164 /** 165 * Removes the specified <code>StatementEventListener</code> from the list of 166 * components that will be notified when the driver detects that a 167 * <code>PreparedStatement</code> has been closed or is invalid. 168 * 169 * @param listener the component which implements the 170 * <code>StatementEventListener</code> interface that was previously 171 * registered with this <code>PooledConnection</code> object 172 * 173 * @since 1.6 174 */ 175 public void removeStatementEventListener(StatementEventListener listener); 176 177 }