2
Copyright (C) 2002-2004 MySQL AB
4
This program is free software; you can redistribute it and/or modify
5
it under the terms of version 2 of the GNU General Public License as
6
published by the Free Software Foundation.
8
There are special exceptions to the terms and conditions of the GPL
9
as it is applied to this software. View the full text of the
10
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11
software distribution.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
package com.mysql.jdbc.jdbc2.optional;
27
import java.sql.Connection;
28
import java.sql.SQLException;
30
import java.util.Enumeration;
31
import java.util.Hashtable;
33
import javax.sql.ConnectionEvent;
34
import javax.sql.ConnectionEventListener;
35
import javax.sql.PooledConnection;
37
import com.mysql.jdbc.SQLError;
40
* This class is used to wrap and return a physical connection within a logical
41
* handle. It also registers and notifies ConnectionEventListeners of any
44
* @see javax.sql.PooledConnection
45
* @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
46
* @author Todd Wolff <todd.wolff_at_prodigy.net>
48
public class MysqlPooledConnection implements PooledConnection {
51
* The flag for an exception being thrown.
53
public static final int CONNECTION_ERROR_EVENT = 1;
56
* The flag for a connection being closed.
58
public static final int CONNECTION_CLOSED_EVENT = 2;
60
// ~ Instance/static variables .............................................
62
private Hashtable eventListeners;
64
private Connection logicalHandle;
66
private com.mysql.jdbc.Connection physicalConn;
68
// ~ Constructors ..........................................................
71
* Construct a new MysqlPooledConnection and set instance variables
74
* physical connection to db
76
public MysqlPooledConnection(com.mysql.jdbc.Connection connection) {
77
this.logicalHandle = null;
78
this.physicalConn = connection;
79
this.eventListeners = new Hashtable(10);
82
// ~ Methods ...............................................................
85
* Adds ConnectionEventListeners to a hash table to be used for notification
88
* @param connectioneventlistener
89
* listener to be notified with ConnectionEvents
91
public synchronized void addConnectionEventListener(
92
ConnectionEventListener connectioneventlistener) {
94
if (this.eventListeners != null) {
95
this.eventListeners.put(connectioneventlistener,
96
connectioneventlistener);
101
* Removes ConnectionEventListeners from hash table used for notification of
104
* @param connectioneventlistener
105
* listener to be removed
107
public synchronized void removeConnectionEventListener(
108
ConnectionEventListener connectioneventlistener) {
110
if (this.eventListeners != null) {
111
this.eventListeners.remove(connectioneventlistener);
116
* Invoked by the container. Return a logicalHandle object that wraps a
117
* physical connection.
119
* @see java.sql.DataSource#getConnection()
121
public synchronized Connection getConnection() throws SQLException {
122
return getConnection(true, false);
126
protected synchronized Connection getConnection(boolean resetServerState,
128
throws SQLException {
129
if (this.physicalConn == null) {
131
SQLException sqlException = SQLError.createSQLException(
132
"Physical Connection doesn't exist");
133
callListener(CONNECTION_ERROR_EVENT, sqlException);
140
if (this.logicalHandle != null) {
141
((ConnectionWrapper) this.logicalHandle).close(false);
144
if (resetServerState) {
145
((com.mysql.jdbc.Connection) this.physicalConn).resetServerState();
148
this.logicalHandle = new ConnectionWrapper(this, this.physicalConn, forXa);
149
} catch (SQLException sqlException) {
150
callListener(CONNECTION_ERROR_EVENT, sqlException);
155
return this.logicalHandle;
159
* Invoked by the container (not the client), and should close the physical
160
* connection. This will be called if the pool is destroyed or the
161
* connectionEventListener receives a connectionErrorOccurred event.
163
* @see java.sql.DataSource#close()
165
public synchronized void close() throws SQLException {
166
if (this.physicalConn != null) {
167
this.physicalConn.close();
170
this.physicalConn = null;
174
* Notifies all registered ConnectionEventListeners of ConnectionEvents.
175
* Instantiates a new ConnectionEvent which wraps sqlException and invokes
176
* either connectionClose or connectionErrorOccurred on listener as
180
* value indicating whether connectionClosed or
181
* connectionErrorOccurred called
182
* @param sqlException
183
* the exception being thrown
185
protected synchronized void callListener(int eventType,
186
SQLException sqlException) {
188
if (this.eventListeners == null) {
193
Enumeration enumeration = this.eventListeners.keys();
194
ConnectionEvent connectionevent = new ConnectionEvent(this,
197
while (enumeration.hasMoreElements()) {
199
ConnectionEventListener connectioneventlistener = (ConnectionEventListener) enumeration
201
ConnectionEventListener connectioneventlistener1 = (ConnectionEventListener) this.eventListeners
202
.get(connectioneventlistener);
204
if (eventType == CONNECTION_CLOSED_EVENT) {
205
connectioneventlistener1.connectionClosed(connectionevent);
206
} else if (eventType == CONNECTION_ERROR_EVENT) {
207
connectioneventlistener1
208
.connectionErrorOccurred(connectionevent);
b'\\ No newline at end of file'