2
Copyright (C) 2002 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation; either version 2 of the License, or
7
(at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
package com.mysql.jdbc.jdbc2.optional;
21
import java.sql.Connection;
22
import java.sql.SQLException;
24
import java.util.Enumeration;
25
import java.util.Hashtable;
27
import javax.sql.ConnectionEvent;
28
import javax.sql.ConnectionEventListener;
29
import javax.sql.PooledConnection;
33
* This class is used to wrap and return a physical connection within a logical handle.
34
* It also registers and notifies ConnectionEventListeners of any ConnectionEvents
36
* @see javax.sql.PooledConnection
37
* @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
38
* @author Todd Wolff <todd.wolff_at_prodigy.net>
40
public class MysqlPooledConnection
41
implements PooledConnection {
44
* The flag for an exception being thrown.
46
public static final int CONNECTION_ERROR_EVENT = 1;
49
* The flag for a connection being closed.
51
public static final int CONNECTION_CLOSED_EVENT = 2;
53
//~ Instance/static variables .............................................
55
private Hashtable eventListeners;
56
private Connection logicalHandle;
57
private Connection physicalConn;
60
//~ Constructors ..........................................................
63
* Construct a new MysqlPooledConnection and set instance variables
65
* @param connection physical connection to db
67
public MysqlPooledConnection(Connection connection) {
69
physicalConn = connection;
70
eventListeners = new Hashtable(10);
73
//~ Methods ...............................................................
76
* Adds ConnectionEventListeners to a hash table to be used for notification of
79
* @param connectioneventlistener listener to be notified with ConnectionEvents
81
public synchronized void addConnectionEventListener(ConnectionEventListener connectioneventlistener) {
83
if (eventListeners != null) {
84
eventListeners.put(connectioneventlistener,
85
connectioneventlistener);
90
* Removes ConnectionEventListeners from hash table used for notification of
93
* @param connectioneventlistener listener to be removed
95
public synchronized void removeConnectionEventListener(ConnectionEventListener connectioneventlistener) {
97
if (eventListeners != null) {
98
eventListeners.remove(connectioneventlistener);
103
* Invoked by the container. Return a logicalHandle object that wraps a physical
106
* @see java.sql.DataSource#getConnection()
108
public synchronized Connection getConnection()
109
throws SQLException {
111
if (physicalConn == null) {
113
SQLException sqlException = new SQLException(
114
"Physical Connection doesn't exist");
115
callListener(CONNECTION_ERROR_EVENT, sqlException);
122
if (logicalHandle != null) {
123
((LogicalHandle)logicalHandle).close(false);
126
logicalHandle = new LogicalHandle(this, physicalConn);
127
} catch (SQLException sqlException) {
128
callListener(CONNECTION_ERROR_EVENT, sqlException);
133
return logicalHandle;
137
* Invoked by the container (not the client), and should close the physical connection.
138
* This will be called if the pool is destroyed or the connectionEventListener receives
139
* a connectionErrorOccurred event.
141
* @see java.sql.DataSource#close()
143
public synchronized void close()
144
throws SQLException {
145
physicalConn.close();
150
* Notifies all registered ConnectionEventListeners of ConnectionEvents. Instantiates
151
* a new ConnectionEvent which wraps sqlException and invokes either connectionClose
152
* or connectionErrorOccurred on listener as appropriate.
154
* @param eventType value indicating whether connectionClosed or connectionErrorOccurred called
155
* @param sqlException the exception being thrown
157
protected synchronized void callListener(int eventType, SQLException sqlException) {
159
if (eventListeners == null) {
164
Enumeration enumeration = eventListeners.keys();
165
ConnectionEvent connectionevent = new ConnectionEvent(this,
168
while (enumeration.hasMoreElements()) {
170
ConnectionEventListener connectioneventlistener =
171
(ConnectionEventListener) enumeration.nextElement();
172
ConnectionEventListener connectioneventlistener1 =
173
(ConnectionEventListener) eventListeners.get(
174
connectioneventlistener);
176
if (eventType == CONNECTION_CLOSED_EVENT) {
177
connectioneventlistener1.connectionClosed(connectionevent);
178
} else if (eventType == CONNECTION_ERROR_EVENT) {
179
connectioneventlistener1.connectionErrorOccurred(