2
Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
5
The MySQL Connector/J is licensed under the terms of the GPLv2
6
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
7
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
8
this software, see the FLOSS License Exception
9
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
11
This program is free software; you can redistribute it and/or modify it under the terms
12
of the GNU General Public License as published by the Free Software Foundation; version 2
15
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
16
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
See the GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License along with this
20
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
21
Floor, Boston, MA 02110-1301 USA
25
package com.mysql.jdbc.jdbc2.optional;
27
import java.lang.reflect.Constructor;
28
import java.sql.Connection;
29
import java.sql.SQLException;
30
import java.util.HashMap;
31
import java.util.Iterator;
34
import javax.sql.ConnectionEvent;
35
import javax.sql.ConnectionEventListener;
36
import javax.sql.PooledConnection;
38
import com.mysql.jdbc.ExceptionInterceptor;
39
import com.mysql.jdbc.SQLError;
40
import com.mysql.jdbc.Util;
43
* This class is used to wrap and return a physical connection within a logical
44
* handle. It also registers and notifies ConnectionEventListeners of any
47
* @see javax.sql.PooledConnection
48
* @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
49
* @author Todd Wolff <todd.wolff_at_prodigy.net>
51
public class MysqlPooledConnection implements PooledConnection {
53
private static final Constructor<?> JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR;
58
JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR = Class.forName(
59
"com.mysql.jdbc.jdbc2.optional.JDBC4MysqlPooledConnection")
61
new Class[] { com.mysql.jdbc.Connection.class });
62
} catch (SecurityException e) {
63
throw new RuntimeException(e);
64
} catch (NoSuchMethodException e) {
65
throw new RuntimeException(e);
66
} catch (ClassNotFoundException e) {
67
throw new RuntimeException(e);
70
JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR = null;
74
protected static MysqlPooledConnection getInstance(com.mysql.jdbc.Connection connection) throws SQLException {
75
if (!Util.isJdbc4()) {
76
return new MysqlPooledConnection(connection);
79
return (MysqlPooledConnection) Util.handleNewInstance(
80
JDBC_4_POOLED_CONNECTION_WRAPPER_CTOR, new Object[] {
81
connection}, connection.getExceptionInterceptor());
85
* The flag for an exception being thrown.
87
public static final int CONNECTION_ERROR_EVENT = 1;
90
* The flag for a connection being closed.
92
public static final int CONNECTION_CLOSED_EVENT = 2;
94
// ~ Instance/static variables .............................................
96
private Map<ConnectionEventListener, ConnectionEventListener> connectionEventListeners;
98
private Connection logicalHandle;
100
private com.mysql.jdbc.Connection physicalConn;
102
private ExceptionInterceptor exceptionInterceptor;
104
// ~ Constructors ..........................................................
107
* Construct a new MysqlPooledConnection and set instance variables
110
* physical connection to db
112
public MysqlPooledConnection(com.mysql.jdbc.Connection connection) {
113
this.logicalHandle = null;
114
this.physicalConn = connection;
115
this.connectionEventListeners = new HashMap<ConnectionEventListener, ConnectionEventListener>();
116
this.exceptionInterceptor = this.physicalConn.getExceptionInterceptor();
120
* Adds ConnectionEventListeners to a hash table to be used for notification
121
* of ConnectionEvents
123
* @param connectioneventlistener
124
* listener to be notified with ConnectionEvents
126
public synchronized void addConnectionEventListener(
127
ConnectionEventListener connectioneventlistener) {
129
if (this.connectionEventListeners != null) {
130
this.connectionEventListeners.put(connectioneventlistener,
131
connectioneventlistener);
136
* Removes ConnectionEventListeners from hash table used for notification of
139
* @param connectioneventlistener
140
* listener to be removed
142
public synchronized void removeConnectionEventListener(
143
ConnectionEventListener connectioneventlistener) {
145
if (this.connectionEventListeners != null) {
146
this.connectionEventListeners.remove(connectioneventlistener);
151
* Invoked by the container. Return a logicalHandle object that wraps a
152
* physical connection.
154
* @see java.sql.DataSource#getConnection()
156
public synchronized Connection getConnection() throws SQLException {
157
return getConnection(true, false);
161
protected synchronized Connection getConnection(boolean resetServerState,
163
throws SQLException {
164
if (this.physicalConn == null) {
166
SQLException sqlException = SQLError.createSQLException(
167
"Physical Connection doesn't exist", this.exceptionInterceptor);
168
callConnectionEventListeners(CONNECTION_ERROR_EVENT, sqlException);
175
if (this.logicalHandle != null) {
176
((ConnectionWrapper) this.logicalHandle).close(false);
179
if (resetServerState) {
180
this.physicalConn.resetServerState();
183
this.logicalHandle = ConnectionWrapper.getInstance(this,
186
} catch (SQLException sqlException) {
187
callConnectionEventListeners(CONNECTION_ERROR_EVENT, sqlException);
192
return this.logicalHandle;
196
* Invoked by the container (not the client), and should close the physical
197
* connection. This will be called if the pool is destroyed or the
198
* connectionEventListener receives a connectionErrorOccurred event.
200
* @see java.sql.DataSource#close()
202
public synchronized void close() throws SQLException {
203
if (this.physicalConn != null) {
204
this.physicalConn.close();
206
this.physicalConn = null;
209
if (this.connectionEventListeners != null) {
210
this.connectionEventListeners.clear();
212
this.connectionEventListeners = null;
217
* Notifies all registered ConnectionEventListeners of ConnectionEvents.
218
* Instantiates a new ConnectionEvent which wraps sqlException and invokes
219
* either connectionClose or connectionErrorOccurred on listener as
223
* value indicating whether connectionClosed or
224
* connectionErrorOccurred called
225
* @param sqlException
226
* the exception being thrown
228
protected synchronized void callConnectionEventListeners(int eventType,
229
SQLException sqlException) {
231
if (this.connectionEventListeners == null) {
236
Iterator<Map.Entry<ConnectionEventListener, ConnectionEventListener>> iterator = this.connectionEventListeners.entrySet().iterator();
238
ConnectionEvent connectionevent = new ConnectionEvent(this,
241
while (iterator.hasNext()) {
243
ConnectionEventListener connectioneventlistener = iterator.next().getValue();
245
if (eventType == CONNECTION_CLOSED_EVENT) {
246
connectioneventlistener.connectionClosed(connectionevent);
247
} else if (eventType == CONNECTION_ERROR_EVENT) {
248
connectioneventlistener
249
.connectionErrorOccurred(connectionevent);
254
protected ExceptionInterceptor getExceptionInterceptor() {
255
return this.exceptionInterceptor;
b'\\ No newline at end of file'