Path: blob/master/src/java.sql/share/classes/javax/sql/PooledConnection.java
41152 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql;2627import java.sql.Connection;28import java.sql.SQLException;2930/**31* An object that provides hooks for connection pool management.32* A {@code PooledConnection} object33* represents a physical connection to a data source. The connection34* can be recycled rather than being closed when an application is35* finished with it, thus reducing the number of connections that36* need to be made.37* <P>38* An application programmer does not use the {@code PooledConnection}39* interface directly; rather, it is used by a middle tier infrastructure40* that manages the pooling of connections.41* <P>42* When an application calls the method {@code DataSource.getConnection},43* it gets back a {@code Connection} object. If connection pooling is44* being done, that {@code Connection} object is actually a handle to45* a {@code PooledConnection} object, which is a physical connection.46* <P>47* The connection pool manager, typically the application server, maintains48* a pool of {@code PooledConnection} objects. If there is a49* {@code PooledConnection} object available in the pool, the50* connection pool manager returns a {@code Connection} object that51* is a handle to that physical connection.52* If no {@code PooledConnection} object is available, the53* connection pool manager calls the {@code ConnectionPoolDataSource}54* method {@code getPoolConnection} to create a new physical connection. The55* JDBC driver implementing {@code ConnectionPoolDataSource} creates a56* new {@code PooledConnection} object and returns a handle to it.57* <P>58* When an application closes a connection, it calls the {@code Connection}59* method {@code close}. When connection pooling is being done,60* the connection pool manager is notified because it has registered itself as61* a {@code ConnectionEventListener} object using the62* {@code ConnectionPool} method {@code addConnectionEventListener}.63* The connection pool manager deactivates the handle to64* the {@code PooledConnection} object and returns the65* {@code PooledConnection} object to the pool of connections so that66* it can be used again. Thus, when an application closes its connection,67* the underlying physical connection is recycled rather than being closed.68* <p>69* If the connection pool manager wraps or provides a proxy to the logical70* handle returned from a call to {@code PoolConnection.getConnection}, the pool71* manager must do one of the following when the connection pool manager72* closes or returns the {@code PooledConnection} to the pool in response to73* the application calling {@code Connection.close}:74* <ul>75* <li>call {@code endRequest} on the logical {@code Connection} handle76* <li>call {@code close} on the logical {@code Connection} handle77* </ul>78* <p>79* The physical connection is not closed until the connection pool manager80* calls the {@code PooledConnection} method {@code close}.81* This method is generally called to have an orderly shutdown of the server or82* if a fatal error has made the connection unusable.83*84* <p>85* A connection pool manager is often also a statement pool manager, maintaining86* a pool of {@code PreparedStatement} objects.87* When an application closes a prepared statement, it calls the88* {@code PreparedStatement}89* method {@code close}. When {@code Statement} pooling is being done,90* the pool manager is notified because it has registered itself as91* a {@code StatementEventListener} object using the92* {@code ConnectionPool} method {@code addStatementEventListener}.93* Thus, when an application closes its {@code PreparedStatement},94* the underlying prepared statement is recycled rather than being closed.95*96* @since 1.497*/9899public interface PooledConnection {100101/**102* Creates and returns a {@code Connection} object that is a handle103* for the physical connection that104* this {@code PooledConnection} object represents.105* The connection pool manager calls this method when an application has106* called the method {@code DataSource.getConnection} and there are107* no {@code PooledConnection} objects available. See the108* {@link PooledConnection interface description} for more information.109*110* @return a {@code Connection} object that is a handle to111* this {@code PooledConnection} object112* @throws SQLException if a database access error occurs113* @throws java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support114* this method115* @since 1.4116*/117Connection getConnection() throws SQLException;118119/**120* Closes the physical connection that this {@code PooledConnection}121* object represents. An application never calls this method directly;122* it is called by the connection pool module, or manager.123* <P>124* See the {@link PooledConnection interface description} for more125* information.126*127* @throws SQLException if a database access error occurs128* @throws java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support129* this method130* @since 1.4131*/132void close() throws SQLException;133134/**135* Registers the given event listener so that it will be notified136* when an event occurs on this {@code PooledConnection} object.137*138* @param listener a component, usually the connection pool manager,139* that has implemented the140* {@code ConnectionEventListener} interface and wants to be141* notified when the connection is closed or has an error142* @see #removeConnectionEventListener143*/144void addConnectionEventListener(ConnectionEventListener listener);145146/**147* Removes the given event listener from the list of components that148* will be notified when an event occurs on this149* {@code PooledConnection} object.150*151* @param listener a component, usually the connection pool manager,152* that has implemented the153* {@code ConnectionEventListener} interface and154* been registered with this {@code PooledConnection} object as155* a listener156* @see #addConnectionEventListener157*/158void removeConnectionEventListener(ConnectionEventListener listener);159160/**161* Registers a {@code StatementEventListener} with this {@code PooledConnection} object. Components that162* wish to be notified when {@code PreparedStatement}s created by the163* connection are closed or are detected to be invalid may use this method164* to register a {@code StatementEventListener} with this {@code PooledConnection} object.165*166* @param listener an component which implements the {@code StatementEventListener}167* interface that is to be registered with this {@code PooledConnection} object168*169* @since 1.6170*/171public void addStatementEventListener(StatementEventListener listener);172173/**174* Removes the specified {@code StatementEventListener} from the list of175* components that will be notified when the driver detects that a176* {@code PreparedStatement} has been closed or is invalid.177*178* @param listener the component which implements the179* {@code StatementEventListener} interface that was previously180* registered with this {@code PooledConnection} object181*182* @since 1.6183*/184public void removeStatementEventListener(StatementEventListener listener);185186}187188189