KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > jdbc2 > optional > MysqlPooledConnection


1 /*
2    Copyright (C) 2002 MySQL AB
3    
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.
8    
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.
13    
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
17       
18  */

19 package com.mysql.jdbc.jdbc2.optional;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 import javax.sql.ConnectionEvent JavaDoc;
28 import javax.sql.ConnectionEventListener JavaDoc;
29 import javax.sql.PooledConnection JavaDoc;
30
31
32 /**
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
35  *
36  * @see javax.sql.PooledConnection
37  * @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle
38  * @author Todd Wolff <todd.wolff_at_prodigy.net>
39  */

40 public class MysqlPooledConnection
41     implements PooledConnection JavaDoc {
42     
43     /**
44      * The flag for an exception being thrown.
45      */

46     public static final int CONNECTION_ERROR_EVENT = 1;
47     
48     /**
49      * The flag for a connection being closed.
50      */

51     public static final int CONNECTION_CLOSED_EVENT = 2;
52     
53     //~ Instance/static variables .............................................
54

55     private Hashtable JavaDoc eventListeners;
56     private Connection JavaDoc logicalHandle;
57     private Connection JavaDoc physicalConn;
58     
59
60     //~ Constructors ..........................................................
61

62     /**
63     * Construct a new MysqlPooledConnection and set instance variables
64     *
65     * @param connection physical connection to db
66     */

67     public MysqlPooledConnection(Connection JavaDoc connection) {
68         logicalHandle = null;
69         physicalConn = connection;
70         eventListeners = new Hashtable JavaDoc(10);
71     }
72
73     //~ Methods ...............................................................
74

75     /**
76      * Adds ConnectionEventListeners to a hash table to be used for notification of
77      * ConnectionEvents
78      *
79      * @param connectioneventlistener listener to be notified with ConnectionEvents
80      */

81     public synchronized void addConnectionEventListener(ConnectionEventListener JavaDoc connectioneventlistener) {
82
83         if (eventListeners != null) {
84             eventListeners.put(connectioneventlistener,
85                                connectioneventlistener);
86         }
87     }
88
89     /**
90      * Removes ConnectionEventListeners from hash table used for notification of
91      * ConnectionEvents
92      *
93      * @param connectioneventlistener listener to be removed
94      */

95     public synchronized void removeConnectionEventListener(ConnectionEventListener JavaDoc connectioneventlistener) {
96
97         if (eventListeners != null) {
98             eventListeners.remove(connectioneventlistener);
99         }
100     }
101
102     /**
103      * Invoked by the container. Return a logicalHandle object that wraps a physical
104      * connection.
105      *
106      * @see java.sql.DataSource#getConnection()
107      */

108     public synchronized Connection JavaDoc getConnection()
109                                           throws SQLException JavaDoc {
110
111         if (physicalConn == null) {
112
113             SQLException JavaDoc sqlException = new SQLException JavaDoc(
114                                                 "Physical Connection doesn't exist");
115             callListener(CONNECTION_ERROR_EVENT, sqlException);
116
117             return null;
118         }
119
120         try {
121
122             if (logicalHandle != null) {
123                 ((ConnectionWrapper)logicalHandle).close(false);
124             }
125
126             logicalHandle = new ConnectionWrapper(this, physicalConn);
127         } catch (SQLException JavaDoc sqlException) {
128             callListener(CONNECTION_ERROR_EVENT, sqlException);
129
130             return null;
131         }
132
133         return logicalHandle;
134     }
135
136     /**
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.
140      *
141      * @see java.sql.DataSource#close()
142      */

143     public synchronized void close()
144                             throws SQLException JavaDoc {
145         physicalConn.close();
146         physicalConn = null;
147     }
148
149     /**
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.
153      *
154      * @param eventType value indicating whether connectionClosed or connectionErrorOccurred called
155      * @param sqlException the exception being thrown
156      */

157     protected synchronized void callListener(int eventType, SQLException JavaDoc sqlException) {
158
159         if (eventListeners == null) {
160
161             return;
162         }
163
164         Enumeration JavaDoc enumeration = eventListeners.keys();
165         ConnectionEvent JavaDoc connectionevent = new ConnectionEvent JavaDoc(this,
166                                                               sqlException);
167
168         while (enumeration.hasMoreElements()) {
169
170             ConnectionEventListener JavaDoc connectioneventlistener =
171                     (ConnectionEventListener JavaDoc) enumeration.nextElement();
172             ConnectionEventListener JavaDoc connectioneventlistener1 =
173                     (ConnectionEventListener JavaDoc) eventListeners.get(
174                             connectioneventlistener);
175
176             if (eventType == CONNECTION_CLOSED_EVENT) {
177                 connectioneventlistener1.connectionClosed(connectionevent);
178             } else if (eventType == CONNECTION_ERROR_EVENT) {
179                 connectioneventlistener1.connectionErrorOccurred(
180                         connectionevent);
181             }
182         }
183     }
184 }
Popular Tags