1 | /* |
---|
2 | Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. |
---|
3 | |
---|
4 | |
---|
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>. |
---|
10 | |
---|
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 |
---|
13 | of the License. |
---|
14 | |
---|
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. |
---|
18 | |
---|
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 |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | */ |
---|
26 | package com.mysql.jdbc.jdbc2.optional; |
---|
27 | |
---|
28 | import java.lang.reflect.InvocationHandler; |
---|
29 | import java.lang.reflect.InvocationTargetException; |
---|
30 | import java.lang.reflect.Method; |
---|
31 | import java.lang.reflect.Proxy; |
---|
32 | import java.sql.Array; |
---|
33 | import java.sql.Blob; |
---|
34 | import java.sql.Clob; |
---|
35 | import java.sql.Connection; |
---|
36 | import java.sql.NClob; |
---|
37 | import java.sql.SQLClientInfoException; |
---|
38 | import java.sql.SQLException; |
---|
39 | import java.sql.SQLXML; |
---|
40 | import java.sql.Savepoint; |
---|
41 | import java.sql.Statement; |
---|
42 | import java.sql.Struct; |
---|
43 | import java.util.ArrayList; |
---|
44 | import java.util.HashMap; |
---|
45 | import java.util.List; |
---|
46 | import java.util.Map; |
---|
47 | import java.util.Properties; |
---|
48 | |
---|
49 | import com.mysql.jdbc.ConnectionImpl; |
---|
50 | import com.mysql.jdbc.SQLError; |
---|
51 | |
---|
52 | /** |
---|
53 | */ |
---|
54 | public class JDBC4ConnectionWrapper extends ConnectionWrapper { |
---|
55 | |
---|
56 | /** |
---|
57 | * Construct a new LogicalHandle and set instance variables |
---|
58 | * |
---|
59 | * @param mysqlPooledConnection |
---|
60 | * reference to object that instantiated this object |
---|
61 | * @param mysqlConnection |
---|
62 | * physical connection to db |
---|
63 | * |
---|
64 | * @throws SQLException |
---|
65 | * if an error occurs. |
---|
66 | */ |
---|
67 | public JDBC4ConnectionWrapper(MysqlPooledConnection mysqlPooledConnection, |
---|
68 | com.mysql.jdbc.Connection mysqlConnection, boolean forXa) |
---|
69 | throws SQLException { |
---|
70 | super(mysqlPooledConnection, mysqlConnection, forXa); |
---|
71 | } |
---|
72 | |
---|
73 | public void close() throws SQLException { |
---|
74 | try { |
---|
75 | super.close(); |
---|
76 | } finally { |
---|
77 | this.unwrappedInterfaces = null; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public SQLXML createSQLXML() throws SQLException { |
---|
82 | checkClosed(); |
---|
83 | |
---|
84 | try { |
---|
85 | return ((java.sql.Connection) this.mc).createSQLXML(); |
---|
86 | } catch (SQLException sqlException) { |
---|
87 | checkAndFireConnectionError(sqlException); |
---|
88 | } |
---|
89 | |
---|
90 | return null; // never reached, but compiler can't tell |
---|
91 | } |
---|
92 | |
---|
93 | public java.sql.Array createArrayOf(String typeName, Object[] elements) |
---|
94 | throws SQLException { |
---|
95 | checkClosed(); |
---|
96 | |
---|
97 | try { |
---|
98 | return ((java.sql.Connection) this.mc).createArrayOf(typeName, |
---|
99 | elements); |
---|
100 | } catch (SQLException sqlException) { |
---|
101 | checkAndFireConnectionError(sqlException); |
---|
102 | } |
---|
103 | |
---|
104 | return null; // never reached, but compiler can't tell |
---|
105 | } |
---|
106 | |
---|
107 | public Struct createStruct(String typeName, Object[] attributes) |
---|
108 | throws SQLException { |
---|
109 | checkClosed(); |
---|
110 | |
---|
111 | try { |
---|
112 | return ((java.sql.Connection) this.mc).createStruct(typeName, |
---|
113 | attributes); |
---|
114 | } catch (SQLException sqlException) { |
---|
115 | checkAndFireConnectionError(sqlException); |
---|
116 | } |
---|
117 | |
---|
118 | return null; // never reached, but compiler can't tell |
---|
119 | } |
---|
120 | |
---|
121 | public Properties getClientInfo() throws SQLException { |
---|
122 | checkClosed(); |
---|
123 | |
---|
124 | try { |
---|
125 | return ((java.sql.Connection) this.mc).getClientInfo(); |
---|
126 | } catch (SQLException sqlException) { |
---|
127 | checkAndFireConnectionError(sqlException); |
---|
128 | } |
---|
129 | |
---|
130 | return null; // never reached, but compiler can't tell |
---|
131 | } |
---|
132 | |
---|
133 | public String getClientInfo(String name) throws SQLException { |
---|
134 | checkClosed(); |
---|
135 | |
---|
136 | try { |
---|
137 | return ((java.sql.Connection) this.mc).getClientInfo(name); |
---|
138 | } catch (SQLException sqlException) { |
---|
139 | checkAndFireConnectionError(sqlException); |
---|
140 | } |
---|
141 | |
---|
142 | return null; // never reached, but compiler can't tell |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Returns true if the connection has not been closed and is still valid. |
---|
147 | * The driver shall submit a query on the connection or use some other |
---|
148 | * mechanism that positively verifies the connection is still valid when |
---|
149 | * this method is called. |
---|
150 | * <p> |
---|
151 | * The query submitted by the driver to validate the connection shall be |
---|
152 | * executed in the context of the current transaction. |
---|
153 | * |
---|
154 | * @param timeout - |
---|
155 | * The time in seconds to wait for the database operation used to |
---|
156 | * validate the connection to complete. If the timeout period |
---|
157 | * expires before the operation completes, this method returns |
---|
158 | * false. A value of 0 indicates a timeout is not applied to the |
---|
159 | * database operation. |
---|
160 | * <p> |
---|
161 | * @return true if the connection is valid, false otherwise |
---|
162 | * @exception SQLException |
---|
163 | * if the value supplied for <code>timeout</code> is less |
---|
164 | * then 0 |
---|
165 | * @since 1.6 |
---|
166 | */ |
---|
167 | public synchronized boolean isValid(int timeout) throws SQLException { |
---|
168 | try { |
---|
169 | return ((java.sql.Connection) this.mc).isValid(timeout); |
---|
170 | } catch (SQLException sqlException) { |
---|
171 | checkAndFireConnectionError(sqlException); |
---|
172 | } |
---|
173 | |
---|
174 | return false; // never reached, but compiler can't tell |
---|
175 | } |
---|
176 | |
---|
177 | public void setClientInfo(Properties properties) |
---|
178 | throws SQLClientInfoException { |
---|
179 | try { |
---|
180 | checkClosed(); |
---|
181 | |
---|
182 | ((java.sql.Connection) this.mc).setClientInfo(properties); |
---|
183 | } catch (SQLException sqlException) { |
---|
184 | try { |
---|
185 | checkAndFireConnectionError(sqlException); |
---|
186 | } catch (SQLException sqlEx2) { |
---|
187 | SQLClientInfoException clientEx = new SQLClientInfoException(); |
---|
188 | clientEx.initCause(sqlEx2); |
---|
189 | |
---|
190 | throw clientEx; |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | public void setClientInfo(String name, String value) |
---|
196 | throws SQLClientInfoException { |
---|
197 | try { |
---|
198 | checkClosed(); |
---|
199 | |
---|
200 | ((java.sql.Connection) this.mc).setClientInfo(name, value); |
---|
201 | } catch (SQLException sqlException) { |
---|
202 | try { |
---|
203 | checkAndFireConnectionError(sqlException); |
---|
204 | } catch (SQLException sqlEx2) { |
---|
205 | SQLClientInfoException clientEx = new SQLClientInfoException(); |
---|
206 | clientEx.initCause(sqlEx2); |
---|
207 | |
---|
208 | throw clientEx; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | /** |
---|
214 | * Returns true if this either implements the interface argument or is |
---|
215 | * directly or indirectly a wrapper for an object that does. Returns false |
---|
216 | * otherwise. If this implements the interface then return true, else if |
---|
217 | * this is a wrapper then return the result of recursively calling |
---|
218 | * <code>isWrapperFor</code> on the wrapped object. If this does not |
---|
219 | * implement the interface and is not a wrapper, return false. This method |
---|
220 | * should be implemented as a low-cost operation compared to |
---|
221 | * <code>unwrap</code> so that callers can use this method to avoid |
---|
222 | * expensive <code>unwrap</code> calls that may fail. If this method |
---|
223 | * returns true then calling <code>unwrap</code> with the same argument |
---|
224 | * should succeed. |
---|
225 | * |
---|
226 | * @param interfaces |
---|
227 | * a Class defining an interface. |
---|
228 | * @return true if this implements the interface or directly or indirectly |
---|
229 | * wraps an object that does. |
---|
230 | * @throws java.sql.SQLException |
---|
231 | * if an error occurs while determining whether this is a |
---|
232 | * wrapper for an object with the given interface. |
---|
233 | * @since 1.6 |
---|
234 | */ |
---|
235 | public boolean isWrapperFor(Class<?> iface) throws SQLException { |
---|
236 | checkClosed(); |
---|
237 | |
---|
238 | boolean isInstance = iface.isInstance(this); |
---|
239 | |
---|
240 | if (isInstance) { |
---|
241 | return true; |
---|
242 | } |
---|
243 | |
---|
244 | return (iface.getName().equals("com.mysql.jdbc.Connection") || |
---|
245 | iface.getName().equals("com.mysql.jdbc.ConnectionProperties")); |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Returns an object that implements the given interface to allow access to |
---|
250 | * non-standard methods, or standard methods not exposed by the proxy. The |
---|
251 | * result may be either the object found to implement the interface or a |
---|
252 | * proxy for that object. If the receiver implements the interface then that |
---|
253 | * is the object. If the receiver is a wrapper and the wrapped object |
---|
254 | * implements the interface then that is the object. Otherwise the object is |
---|
255 | * the result of calling <code>unwrap</code> recursively on the wrapped |
---|
256 | * object. If the receiver is not a wrapper and does not implement the |
---|
257 | * interface, then an <code>SQLException</code> is thrown. |
---|
258 | * |
---|
259 | * @param iface |
---|
260 | * A Class defining an interface that the result must implement. |
---|
261 | * @return an object that implements the interface. May be a proxy for the |
---|
262 | * actual implementing object. |
---|
263 | * @throws java.sql.SQLException |
---|
264 | * If no object found that implements the interface |
---|
265 | * @since 1.6 |
---|
266 | */ |
---|
267 | public synchronized <T> T unwrap(java.lang.Class<T> iface) |
---|
268 | throws java.sql.SQLException { |
---|
269 | try { |
---|
270 | if ("java.sql.Connection".equals(iface.getName()) |
---|
271 | || "java.sql.Wrapper.class".equals(iface.getName())) { |
---|
272 | return iface.cast(this); |
---|
273 | } |
---|
274 | |
---|
275 | if (unwrappedInterfaces == null) { |
---|
276 | unwrappedInterfaces = new HashMap(); |
---|
277 | } |
---|
278 | |
---|
279 | Object cachedUnwrapped = unwrappedInterfaces.get(iface); |
---|
280 | |
---|
281 | if (cachedUnwrapped == null) { |
---|
282 | cachedUnwrapped = Proxy.newProxyInstance(this.mc.getClass() |
---|
283 | .getClassLoader(), new Class[] { iface }, |
---|
284 | new ConnectionErrorFiringInvocationHandler(this.mc)); |
---|
285 | unwrappedInterfaces.put(iface, cachedUnwrapped); |
---|
286 | } |
---|
287 | |
---|
288 | return iface.cast(cachedUnwrapped); |
---|
289 | } catch (ClassCastException cce) { |
---|
290 | throw SQLError.createSQLException("Unable to unwrap to " |
---|
291 | + iface.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor); |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @see java.sql.Connection#createBlob() |
---|
297 | */ |
---|
298 | public Blob createBlob() throws SQLException { |
---|
299 | checkClosed(); |
---|
300 | |
---|
301 | try { |
---|
302 | return ((java.sql.Connection) this.mc).createBlob(); |
---|
303 | } catch (SQLException sqlException) { |
---|
304 | checkAndFireConnectionError(sqlException); |
---|
305 | } |
---|
306 | |
---|
307 | return null; // never reached, but compiler can't tell |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * @see java.sql.Connection#createClob() |
---|
312 | */ |
---|
313 | public Clob createClob() throws SQLException { |
---|
314 | checkClosed(); |
---|
315 | |
---|
316 | try { |
---|
317 | return ((java.sql.Connection) this.mc).createClob(); |
---|
318 | } catch (SQLException sqlException) { |
---|
319 | checkAndFireConnectionError(sqlException); |
---|
320 | } |
---|
321 | |
---|
322 | return null; // never reached, but compiler can't tell |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * @see java.sql.Connection#createNClob() |
---|
327 | */ |
---|
328 | public NClob createNClob() throws SQLException { |
---|
329 | checkClosed(); |
---|
330 | |
---|
331 | try { |
---|
332 | return ((java.sql.Connection) this.mc).createNClob(); |
---|
333 | } catch (SQLException sqlException) { |
---|
334 | checkAndFireConnectionError(sqlException); |
---|
335 | } |
---|
336 | |
---|
337 | return null; // never reached, but compiler can't tell |
---|
338 | } |
---|
339 | } |
---|