1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
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. |
7 | |
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. |
12 | |
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. |
17 | |
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 |
21 | |
22 | |
23 | |
24 | */ |
25 | package com.mysql.jdbc.jdbc2.optional; |
26 | |
27 | import java.sql.Connection; |
28 | import java.sql.SQLException; |
29 | |
30 | import java.util.Enumeration; |
31 | import java.util.Hashtable; |
32 | |
33 | import javax.sql.ConnectionEvent; |
34 | import javax.sql.ConnectionEventListener; |
35 | import javax.sql.PooledConnection; |
36 | |
37 | import com.mysql.jdbc.SQLError; |
38 | |
39 | /** |
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 |
42 | * ConnectionEvents |
43 | * |
44 | * @see javax.sql.PooledConnection |
45 | * @see org.gjt.mm.mysql.jdbc2.optional.LogicalHandle |
46 | * @author Todd Wolff <todd.wolff_at_prodigy.net> |
47 | */ |
48 | public class MysqlPooledConnection implements PooledConnection { |
49 | |
50 | /** |
51 | * The flag for an exception being thrown. |
52 | */ |
53 | public static final int CONNECTION_ERROR_EVENT = 1; |
54 | |
55 | /** |
56 | * The flag for a connection being closed. |
57 | */ |
58 | public static final int CONNECTION_CLOSED_EVENT = 2; |
59 | |
60 | // ~ Instance/static variables ............................................. |
61 | |
62 | private Hashtable eventListeners; |
63 | |
64 | private Connection logicalHandle; |
65 | |
66 | private com.mysql.jdbc.Connection physicalConn; |
67 | |
68 | // ~ Constructors .......................................................... |
69 | |
70 | /** |
71 | * Construct a new MysqlPooledConnection and set instance variables |
72 | * |
73 | * @param connection |
74 | * physical connection to db |
75 | */ |
76 | public MysqlPooledConnection(com.mysql.jdbc.Connection connection) { |
77 | this.logicalHandle = null; |
78 | this.physicalConn = connection; |
79 | this.eventListeners = new Hashtable(10); |
80 | } |
81 | |
82 | // ~ Methods ............................................................... |
83 | |
84 | /** |
85 | * Adds ConnectionEventListeners to a hash table to be used for notification |
86 | * of ConnectionEvents |
87 | * |
88 | * @param connectioneventlistener |
89 | * listener to be notified with ConnectionEvents |
90 | */ |
91 | public synchronized void addConnectionEventListener( |
92 | ConnectionEventListener connectioneventlistener) { |
93 | |
94 | if (this.eventListeners != null) { |
95 | this.eventListeners.put(connectioneventlistener, |
96 | connectioneventlistener); |
97 | } |
98 | } |
99 | |
100 | /** |
101 | * Removes ConnectionEventListeners from hash table used for notification of |
102 | * ConnectionEvents |
103 | * |
104 | * @param connectioneventlistener |
105 | * listener to be removed |
106 | */ |
107 | public synchronized void removeConnectionEventListener( |
108 | ConnectionEventListener connectioneventlistener) { |
109 | |
110 | if (this.eventListeners != null) { |
111 | this.eventListeners.remove(connectioneventlistener); |
112 | } |
113 | } |
114 | |
115 | /** |
116 | * Invoked by the container. Return a logicalHandle object that wraps a |
117 | * physical connection. |
118 | * |
119 | * @see java.sql.DataSource#getConnection() |
120 | */ |
121 | public synchronized Connection getConnection() throws SQLException { |
122 | return getConnection(true, false); |
123 | |
124 | } |
125 | |
126 | protected synchronized Connection getConnection(boolean resetServerState, |
127 | boolean forXa) |
128 | throws SQLException { |
129 | if (this.physicalConn == null) { |
130 | |
131 | SQLException sqlException = SQLError.createSQLException( |
132 | "Physical Connection doesn't exist"); |
133 | callListener(CONNECTION_ERROR_EVENT, sqlException); |
134 | |
135 | throw sqlException; |
136 | } |
137 | |
138 | try { |
139 | |
140 | if (this.logicalHandle != null) { |
141 | ((ConnectionWrapper) this.logicalHandle).close(false); |
142 | } |
143 | |
144 | if (resetServerState) { |
145 | ((com.mysql.jdbc.Connection) this.physicalConn).resetServerState(); |
146 | } |
147 | |
148 | this.logicalHandle = new ConnectionWrapper(this, this.physicalConn, forXa); |
149 | } catch (SQLException sqlException) { |
150 | callListener(CONNECTION_ERROR_EVENT, sqlException); |
151 | |
152 | throw sqlException; |
153 | } |
154 | |
155 | return this.logicalHandle; |
156 | } |
157 | |
158 | /** |
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. |
162 | * |
163 | * @see java.sql.DataSource#close() |
164 | */ |
165 | public synchronized void close() throws SQLException { |
166 | if (this.physicalConn != null) { |
167 | this.physicalConn.close(); |
168 | } |
169 | |
170 | this.physicalConn = null; |
171 | } |
172 | |
173 | /** |
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 |
177 | * appropriate. |
178 | * |
179 | * @param eventType |
180 | * value indicating whether connectionClosed or |
181 | * connectionErrorOccurred called |
182 | * @param sqlException |
183 | * the exception being thrown |
184 | */ |
185 | protected synchronized void callListener(int eventType, |
186 | SQLException sqlException) { |
187 | |
188 | if (this.eventListeners == null) { |
189 | |
190 | return; |
191 | } |
192 | |
193 | Enumeration enumeration = this.eventListeners.keys(); |
194 | ConnectionEvent connectionevent = new ConnectionEvent(this, |
195 | sqlException); |
196 | |
197 | while (enumeration.hasMoreElements()) { |
198 | |
199 | ConnectionEventListener connectioneventlistener = (ConnectionEventListener) enumeration |
200 | .nextElement(); |
201 | ConnectionEventListener connectioneventlistener1 = (ConnectionEventListener) this.eventListeners |
202 | .get(connectioneventlistener); |
203 | |
204 | if (eventType == CONNECTION_CLOSED_EVENT) { |
205 | connectioneventlistener1.connectionClosed(connectionevent); |
206 | } else if (eventType == CONNECTION_ERROR_EVENT) { |
207 | connectioneventlistener1 |
208 | .connectionErrorOccurred(connectionevent); |
209 | } |
210 | } |
211 | } |
212 | } |