blob: 83e35b41a852d80c608b50e49ea9baaeee214866 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
Lance Andersen5862a632017-04-20 13:57:44 -04002 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
J. Duke319a3b92007-12-01 00:00:00 +00003 * 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
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
J. Duke319a3b92007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
Kelly O'Hairfe008ae2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
J. Duke319a3b92007-12-01 00:00:00 +000010 *
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 *
Kelly O'Hairfe008ae2010-05-25 15:58:33 -070021 * 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.
J. Duke319a3b92007-12-01 00:00:00 +000024 */
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.
Lance Andersenf10554d2015-11-05 10:37:08 -050069 * <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
Lance Andersen5862a632017-04-20 13:57:44 -040072 * 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}:
Lance Andersenf10554d2015-11-05 10:37:08 -050075 * <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>
J. Duke319a3b92007-12-01 00:00:00 +000080 * 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>
Sergey Malenkovc500ed62013-10-29 17:01:06 +040086 * A connection pool manager is often also a statement pool manager, maintaining
J. Duke319a3b92007-12-01 00:00:00 +000087 * 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.
J. Duke319a3b92007-12-01 00:00:00 +000096 *
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
Alan Bateman9b8e06e2012-10-01 15:36:57 +0100114 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support
J. Duke319a3b92007-12-01 00:00:00 +0000115 * 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
Alan Bateman9b8e06e2012-10-01 15:36:57 +0100129 * @exception java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support
J. Duke319a3b92007-12-01 00:00:00 +0000130 * 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.
Alexander Stepanov31e76a92014-04-30 15:13:44 +0400166 *
J. Duke319a3b92007-12-01 00:00:00 +0000167 * @param listener an component which implements the <code>StatementEventListener</code>
168 * interface that is to be registered with this <code>PooledConnection</code> object
Alexander Stepanov31e76a92014-04-30 15:13:44 +0400169 *
J. Duke319a3b92007-12-01 00:00:00 +0000170 * @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.
Alexander Stepanov31e76a92014-04-30 15:13:44 +0400178 *
J. Duke319a3b92007-12-01 00:00:00 +0000179 * @param listener the component which implements the
180 * <code>StatementEventListener</code> interface that was previously
181 * registered with this <code>PooledConnection</code> object
Alexander Stepanov31e76a92014-04-30 15:13:44 +0400182 *
J. Duke319a3b92007-12-01 00:00:00 +0000183 * @since 1.6
184 */
185 public void removeStatementEventListener(StatementEventListener listener);
186
187 }