1/* 2 * Copyright (c) 2000, 2017, 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 26package javax.sql; 27 28import java.sql.Connection; 29import 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 * If the connection pool manager wraps or provides a proxy to the logical 71 * handle returned from a call to {@code PoolConnection.getConnection}, the pool 72 * manager must do one of the following when the connection pool manager 73 * closes or returns the {@code PooledConnection} to the pool in response to 74 * the application calling {@code Connection.close}: 75 * <ul> 76 * <li>call {@code endRequest} on the logical {@code Connection} handle 77 * <li>call {@code close} on the logical {@code Connection} handle 78 * </ul> 79 * <p> 80 * The physical connection is not closed until the connection pool manager 81 * calls the <code>PooledConnection</code> method <code>close</code>. 82 * This method is generally called to have an orderly shutdown of the server or 83 * if a fatal error has made the connection unusable. 84 * 85 * <p> 86 * A connection pool manager is often also a statement pool manager, maintaining 87 * a pool of <code>PreparedStatement</code> objects. 88 * When an application closes a prepared statement, it calls the 89 * <code>PreparedStatement</code> 90 * method <code>close</code>. When <code>Statement</code> pooling is being done, 91 * the pool manager is notified because it has registered itself as 92 * a <code>StatementEventListener</code> object using the 93 * <code>ConnectionPool</code> method <code>addStatementEventListener</code>. 94 * Thus, when an application closes its <code>PreparedStatement</code>, 95 * the underlying prepared statement is recycled rather than being closed. 96 * 97 * @since 1.4 98 */ 99 100public interface PooledConnection { 101 102 /** 103 * Creates and returns a <code>Connection</code> object that is a handle 104 * for the physical connection that 105 * this <code>PooledConnection</code> object represents. 106 * The connection pool manager calls this method when an application has 107 * called the method <code>DataSource.getConnection</code> and there are 108 * no <code>PooledConnection</code> objects available. See the 109 * {@link PooledConnection interface description} for more information. 110 * 111 * @return a <code>Connection</code> object that is a handle to 112 * this <code>PooledConnection</code> object 113 * @exception SQLException if a database access error occurs 114 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support 115 * this method 116 * @since 1.4 117 */ 118 Connection getConnection() throws SQLException; 119 120 /** 121 * Closes the physical connection that this <code>PooledConnection</code> 122 * object represents. An application never calls this method directly; 123 * it is called by the connection pool module, or manager. 124 * <P> 125 * See the {@link PooledConnection interface description} for more 126 * information. 127 * 128 * @exception SQLException if a database access error occurs 129 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support 130 * this method 131 * @since 1.4 132 */ 133 void close() throws SQLException; 134 135 /** 136 * Registers the given event listener so that it will be notified 137 * when an event occurs on this <code>PooledConnection</code> object. 138 * 139 * @param listener a component, usually the connection pool manager, 140 * that has implemented the 141 * <code>ConnectionEventListener</code> interface and wants to be 142 * notified when the connection is closed or has an error 143 * @see #removeConnectionEventListener 144 */ 145 void addConnectionEventListener(ConnectionEventListener listener); 146 147 /** 148 * Removes the given event listener from the list of components that 149 * will be notified when an event occurs on this 150 * <code>PooledConnection</code> object. 151 * 152 * @param listener a component, usually the connection pool manager, 153 * that has implemented the 154 * <code>ConnectionEventListener</code> interface and 155 * been registered with this <code>PooledConnection</code> object as 156 * a listener 157 * @see #addConnectionEventListener 158 */ 159 void removeConnectionEventListener(ConnectionEventListener listener); 160 161 /** 162 * Registers a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. Components that 163 * wish to be notified when <code>PreparedStatement</code>s created by the 164 * connection are closed or are detected to be invalid may use this method 165 * to register a <code>StatementEventListener</code> with this <code>PooledConnection</code> object. 166 * 167 * @param listener an component which implements the <code>StatementEventListener</code> 168 * interface that is to be registered with this <code>PooledConnection</code> object 169 * 170 * @since 1.6 171 */ 172 public void addStatementEventListener(StatementEventListener listener); 173 174 /** 175 * Removes the specified <code>StatementEventListener</code> from the list of 176 * components that will be notified when the driver detects that a 177 * <code>PreparedStatement</code> has been closed or is invalid. 178 * 179 * @param listener the component which implements the 180 * <code>StatementEventListener</code> interface that was previously 181 * registered with this <code>PooledConnection</code> object 182 * 183 * @since 1.6 184 */ 185 public void removeStatementEventListener(StatementEventListener listener); 186 187 } 188