1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.jdbc.BrokeredConnection |
4 | |
5 | Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.jdbc; |
22 | |
23 | import java.sql.Connection; |
24 | import java.sql.Statement; |
25 | import java.sql.PreparedStatement; |
26 | import java.sql.CallableStatement; |
27 | import java.sql.DatabaseMetaData; |
28 | import java.sql.SQLException; |
29 | import java.sql.SQLWarning; |
30 | |
31 | import org.apache.derby.impl.jdbc.EmbedSQLWarning; |
32 | import org.apache.derby.impl.jdbc.Util; |
33 | |
34 | import java.io.ObjectOutput; |
35 | import java.io.ObjectInput; |
36 | |
37 | import java.lang.reflect.*; |
38 | |
39 | import org.apache.derby.iapi.reference.JDBC30Translation; |
40 | import org.apache.derby.iapi.error.PublicAPI; |
41 | import org.apache.derby.iapi.error.StandardException; |
42 | import org.apache.derby.shared.common.reference.SQLState; |
43 | |
44 | /** |
45 | * This is a rudimentary connection that delegates |
46 | * EVERYTHING to Connection. |
47 | */ |
48 | public class BrokeredConnection implements EngineConnection |
49 | { |
50 | |
51 | // default for Derby |
52 | int stateHoldability = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT; |
53 | |
54 | final BrokeredConnectionControl control; |
55 | private boolean isClosed; |
56 | private String connString; |
57 | |
58 | /** |
59 | Maintain state as seen by this Connection handle, not the state |
60 | of the underlying Connection it is attached to. |
61 | */ |
62 | private int stateIsolationLevel; |
63 | private boolean stateReadOnly; |
64 | private boolean stateAutoCommit; |
65 | |
66 | ///////////////////////////////////////////////////////////////////////// |
67 | // |
68 | // CONSTRUCTORS |
69 | // |
70 | ///////////////////////////////////////////////////////////////////////// |
71 | |
72 | public BrokeredConnection(BrokeredConnectionControl control) |
73 | { |
74 | this.control = control; |
75 | } |
76 | |
77 | public final void setAutoCommit(boolean autoCommit) throws SQLException |
78 | { |
79 | try { |
80 | control.checkAutoCommit(autoCommit); |
81 | |
82 | getRealConnection().setAutoCommit(autoCommit); |
83 | |
84 | stateAutoCommit = autoCommit; |
85 | } catch (SQLException sqle) { |
86 | notifyException(sqle); |
87 | throw sqle; |
88 | } |
89 | } |
90 | public final boolean getAutoCommit() throws SQLException |
91 | { |
92 | try { |
93 | return getRealConnection().getAutoCommit(); |
94 | } catch (SQLException sqle) { |
95 | notifyException(sqle); |
96 | throw sqle; |
97 | } |
98 | } |
99 | public final Statement createStatement() throws SQLException |
100 | { |
101 | try { |
102 | return control.wrapStatement(getRealConnection().createStatement()); |
103 | } catch (SQLException sqle) { |
104 | notifyException(sqle); |
105 | throw sqle; |
106 | } |
107 | } |
108 | |
109 | public final PreparedStatement prepareStatement(String sql) |
110 | throws SQLException |
111 | { |
112 | try { |
113 | return control.wrapStatement(getRealConnection().prepareStatement(sql), sql, null); |
114 | } catch (SQLException sqle) { |
115 | notifyException(sqle); |
116 | throw sqle; |
117 | } |
118 | } |
119 | |
120 | public final CallableStatement prepareCall(String sql) throws SQLException |
121 | { |
122 | try { |
123 | return control.wrapStatement(getRealConnection().prepareCall(sql), sql); |
124 | } catch (SQLException sqle) { |
125 | notifyException(sqle); |
126 | throw sqle; |
127 | } |
128 | } |
129 | |
130 | public final String nativeSQL(String sql) throws SQLException |
131 | { |
132 | try { |
133 | return getRealConnection().nativeSQL(sql); |
134 | } catch (SQLException sqle) { |
135 | notifyException(sqle); |
136 | throw sqle; |
137 | } |
138 | } |
139 | |
140 | public final void commit() throws SQLException |
141 | { |
142 | try { |
143 | control.checkCommit(); |
144 | getRealConnection().commit(); |
145 | } catch (SQLException sqle) { |
146 | notifyException(sqle); |
147 | throw sqle; |
148 | } |
149 | } |
150 | |
151 | public final void rollback() throws SQLException |
152 | { |
153 | try { |
154 | control.checkRollback(); |
155 | getRealConnection().rollback(); |
156 | } catch (SQLException sqle) { |
157 | notifyException(sqle); |
158 | throw sqle; |
159 | } |
160 | } |
161 | |
162 | public final void close() throws SQLException |
163 | { |
164 | if (isClosed) |
165 | return; |
166 | |
167 | try { |
168 | if (!control.closingConnection()) { |
169 | isClosed = true; |
170 | return; |
171 | } |
172 | |
173 | isClosed = true; |
174 | |
175 | |
176 | getRealConnection().close(); |
177 | } catch (SQLException sqle) { |
178 | notifyException(sqle); |
179 | throw sqle; |
180 | } |
181 | } |
182 | |
183 | public final boolean isClosed() throws SQLException |
184 | { |
185 | if (isClosed) |
186 | return true; |
187 | try { |
188 | boolean realIsClosed = getRealConnection().isClosed(); |
189 | if (realIsClosed) { |
190 | control.closingConnection(); |
191 | isClosed = true; |
192 | } |
193 | return realIsClosed; |
194 | } catch (SQLException sqle) { |
195 | notifyException(sqle); |
196 | throw sqle; |
197 | } |
198 | } |
199 | |
200 | public final SQLWarning getWarnings() throws SQLException |
201 | { |
202 | try { |
203 | return getRealConnection().getWarnings(); |
204 | } catch (SQLException sqle) { |
205 | notifyException(sqle); |
206 | throw sqle; |
207 | } |
208 | } |
209 | |
210 | public final void clearWarnings() throws SQLException |
211 | { |
212 | try { |
213 | getRealConnection().clearWarnings(); |
214 | } catch (SQLException sqle) { |
215 | notifyException(sqle); |
216 | throw sqle; |
217 | } |
218 | } |
219 | |
220 | public final DatabaseMetaData getMetaData() throws SQLException |
221 | { |
222 | try { |
223 | return getRealConnection().getMetaData(); |
224 | } catch (SQLException sqle) { |
225 | notifyException(sqle); |
226 | throw sqle; |
227 | } |
228 | } |
229 | |
230 | public final void setReadOnly(boolean readOnly) throws SQLException |
231 | { |
232 | try { |
233 | getRealConnection().setReadOnly(readOnly); |
234 | stateReadOnly = readOnly; |
235 | } catch (SQLException sqle) { |
236 | notifyException(sqle); |
237 | throw sqle; |
238 | } |
239 | } |
240 | |
241 | public final boolean isReadOnly() throws SQLException |
242 | { |
243 | try { |
244 | return getRealConnection().isReadOnly(); |
245 | } catch (SQLException sqle) { |
246 | notifyException(sqle); |
247 | throw sqle; |
248 | } |
249 | } |
250 | |
251 | public final void setCatalog(String catalog) throws SQLException |
252 | { |
253 | try { |
254 | getRealConnection().setCatalog(catalog); |
255 | } catch (SQLException sqle) { |
256 | notifyException(sqle); |
257 | throw sqle; |
258 | } |
259 | } |
260 | |
261 | public final String getCatalog() throws SQLException |
262 | { |
263 | try { |
264 | return getRealConnection().getCatalog(); |
265 | } catch (SQLException sqle) { |
266 | notifyException(sqle); |
267 | throw sqle; |
268 | } |
269 | } |
270 | |
271 | public final void setTransactionIsolation(int level) throws SQLException |
272 | { |
273 | try { |
274 | getRealConnection().setTransactionIsolation(level); |
275 | stateIsolationLevel = level; |
276 | } catch (SQLException sqle) { |
277 | notifyException(sqle); |
278 | throw sqle; |
279 | } |
280 | } |
281 | |
282 | public final int getTransactionIsolation() throws SQLException |
283 | { |
284 | try { |
285 | return getRealConnection().getTransactionIsolation(); |
286 | } catch (SQLException sqle) { |
287 | notifyException(sqle); |
288 | throw sqle; |
289 | } |
290 | } |
291 | |
292 | public final Statement createStatement(int resultSetType, int resultSetConcurrency) |
293 | throws SQLException |
294 | { |
295 | try |
296 | { |
297 | return control.wrapStatement(getRealConnection(). |
298 | createStatement(resultSetType, resultSetConcurrency)); |
299 | } |
300 | catch (SQLException se) |
301 | { |
302 | notifyException(se); |
303 | throw se; |
304 | } |
305 | } |
306 | |
307 | |
308 | public final PreparedStatement prepareStatement(String sql, int resultSetType, |
309 | int resultSetConcurrency) |
310 | throws SQLException |
311 | { |
312 | try |
313 | { |
314 | return control.wrapStatement(getRealConnection(). |
315 | prepareStatement(sql, resultSetType, resultSetConcurrency), sql, null); |
316 | } |
317 | catch (SQLException se) |
318 | { |
319 | notifyException(se); |
320 | throw se; |
321 | } |
322 | } |
323 | |
324 | public final CallableStatement prepareCall(String sql, int resultSetType, |
325 | int resultSetConcurrency) throws SQLException |
326 | { |
327 | try |
328 | { |
329 | return control.wrapStatement(getRealConnection(). |
330 | prepareCall(sql, resultSetType, resultSetConcurrency), sql); |
331 | } |
332 | catch (SQLException se) |
333 | { |
334 | notifyException(se); |
335 | throw se; |
336 | } |
337 | } |
338 | |
339 | public java.util.Map getTypeMap() throws SQLException |
340 | { |
341 | try |
342 | { |
343 | return getRealConnection().getTypeMap(); |
344 | } |
345 | catch (SQLException se) |
346 | { |
347 | notifyException(se); |
348 | throw se; |
349 | } |
350 | } |
351 | |
352 | public final void setTypeMap(java.util.Map map) throws SQLException |
353 | { |
354 | try |
355 | { |
356 | getRealConnection().setTypeMap(map); |
357 | } |
358 | catch (SQLException se) |
359 | { |
360 | notifyException(se); |
361 | throw se; |
362 | } |
363 | } |
364 | |
365 | ///////////////////////////////////////////////////////////////////////// |
366 | // |
367 | // MINIONS |
368 | // |
369 | ///////////////////////////////////////////////////////////////////////// |
370 | |
371 | /** |
372 | * A little indirection for getting the real connection. |
373 | * |
374 | * @return the current connection |
375 | */ |
376 | final EngineConnection getRealConnection() throws SQLException { |
377 | if (isClosed) |
378 | throw Util.noCurrentConnection(); |
379 | |
380 | return control.getRealConnection(); |
381 | } |
382 | |
383 | final void notifyException(SQLException sqle) { |
384 | if (!isClosed) |
385 | control.notifyException(sqle); |
386 | } |
387 | |
388 | /** |
389 | Sync up the state of the underlying connection |
390 | with the state of this new handle. |
391 | */ |
392 | public void syncState() throws SQLException { |
393 | EngineConnection conn = getRealConnection(); |
394 | |
395 | stateIsolationLevel = conn.getTransactionIsolation(); |
396 | stateReadOnly = conn.isReadOnly(); |
397 | stateAutoCommit = conn.getAutoCommit(); |
398 | stateHoldability = conn.getHoldability(); |
399 | } |
400 | |
401 | /** |
402 | Isolation level state in BrokeredConnection can get out of sync |
403 | if the isolation is set using SQL rather than JDBC. In order to |
404 | ensure correct state level information, this method is called |
405 | at the start and end of a global transaction. |
406 | */ |
407 | public void getIsolationUptoDate() throws SQLException { |
408 | if (control.isIsolationLevelSetUsingSQLorJDBC()) { |
409 | stateIsolationLevel = getRealConnection().getTransactionIsolation(); |
410 | control.resetIsolationLevelFlag(); |
411 | } |
412 | } |
413 | /** |
414 | Set the state of the underlying connection according to the |
415 | state of this connection's view of state. |
416 | |
417 | @param complete If true set the complete state of the underlying |
418 | Connection, otherwise set only the Connection related state (ie. |
419 | the non-transaction specific state). |
420 | |
421 | |
422 | */ |
423 | public void setState(boolean complete) throws SQLException { |
424 | Class[] CONN_PARAM = { Integer.TYPE }; |
425 | Object[] CONN_ARG = { new Integer(stateHoldability)}; |
426 | |
427 | Connection conn = getRealConnection(); |
428 | |
429 | if (complete) { |
430 | conn.setTransactionIsolation(stateIsolationLevel); |
431 | conn.setReadOnly(stateReadOnly); |
432 | conn.setAutoCommit(stateAutoCommit); |
433 | // make the underlying connection pick my holdability state |
434 | // since holdability is a state of the connection handle |
435 | // not the underlying transaction. |
436 | // jdk13 does not have Connection.setHoldability method and hence using |
437 | // reflection to cover both jdk13 and higher jdks |
438 | try { |
439 | Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM); |
440 | sh.invoke(conn, CONN_ARG); |
441 | } catch( Exception e) |
442 | { |
443 | throw PublicAPI.wrapStandardException( StandardException.plainWrapException( e)); |
444 | } |
445 | } |
446 | } |
447 | |
448 | public BrokeredStatement newBrokeredStatement(BrokeredStatementControl statementControl) throws SQLException { |
449 | return new BrokeredStatement(statementControl, getJDBCLevel()); |
450 | } |
451 | public BrokeredPreparedStatement newBrokeredStatement(BrokeredStatementControl statementControl, String sql, Object generatedKeys) throws SQLException { |
452 | return new BrokeredPreparedStatement(statementControl, getJDBCLevel(), sql); |
453 | } |
454 | public BrokeredCallableStatement newBrokeredStatement(BrokeredStatementControl statementControl, String sql) throws SQLException { |
455 | return new BrokeredCallableStatement(statementControl, getJDBCLevel(), sql); |
456 | } |
457 | |
458 | /** |
459 | * set the DrdaId for this connection. The drdaID prints with the |
460 | * statement text to the errror log |
461 | * @param drdaID drdaID to be used for this connection |
462 | * |
463 | */ |
464 | public final void setDrdaID(String drdaID) |
465 | { |
466 | try { |
467 | getRealConnection().setDrdaID(drdaID); |
468 | } catch (SQLException sqle) |
469 | { |
470 | // connection is closed, just ignore drdaId |
471 | // since connection cannot be used. |
472 | } |
473 | } |
474 | |
475 | /** |
476 | * Set the internal isolation level to use for preparing statements. |
477 | * Subsequent prepares will use this isoalation level |
478 | * @param level - internal isolation level |
479 | * @throws SQLException |
480 | * See EmbedConnection#setPrepareIsolation |
481 | * |
482 | */ |
483 | public final void setPrepareIsolation(int level) throws SQLException |
484 | { |
485 | getRealConnection().setPrepareIsolation(level); |
486 | } |
487 | |
488 | /** |
489 | * get the isolation level that is currently being used to prepare |
490 | * statements (used for network server) |
491 | * |
492 | * @throws SQLException |
493 | * @return current prepare isolation level |
494 | * See EmbedConnection#getPrepareIsolation |
495 | */ |
496 | public final int getPrepareIsolation() throws SQLException |
497 | { |
498 | return getRealConnection().getPrepareIsolation(); |
499 | } |
500 | |
501 | /** |
502 | * Add a SQLWarning to this Connection object. |
503 | * @throws SQLException |
504 | */ |
505 | public final void addWarning(SQLWarning w) throws SQLException |
506 | { |
507 | getRealConnection().addWarning(w); |
508 | } |
509 | |
510 | /** |
511 | * Checks if the connection is closed and throws an exception if |
512 | * it is. |
513 | * |
514 | * @exception SQLException if the connection is closed |
515 | */ |
516 | protected final void checkIfClosed() throws SQLException { |
517 | if (isClosed()) { |
518 | throw Util.noCurrentConnection(); |
519 | } |
520 | } |
521 | |
522 | /** |
523 | * Get the string representation for this connection. Return |
524 | * the class name/hash code and various debug information. |
525 | * |
526 | * @return unique string representation for this connection |
527 | */ |
528 | public String toString() |
529 | { |
530 | if ( connString == null ) |
531 | { |
532 | String wrappedString; |
533 | try |
534 | { |
535 | wrappedString = getRealConnection().toString(); |
536 | } |
537 | catch ( SQLException e ) |
538 | { |
539 | wrappedString = "<none>"; |
540 | } |
541 | |
542 | connString = this.getClass().getName() + "@" + this.hashCode() + |
543 | ", Wrapped Connection = " + wrappedString; |
544 | } |
545 | |
546 | return connString; |
547 | } |
548 | |
549 | int getJDBCLevel() { return 2;} |
550 | |
551 | /* |
552 | * JDBC 3.0 methods that are exposed through EngineConnection. |
553 | */ |
554 | |
555 | /** |
556 | * Prepare statement with explicit holdability. |
557 | */ |
558 | public final PreparedStatement prepareStatement(String sql, |
559 | int resultSetType, int resultSetConcurrency, |
560 | int resultSetHoldability) throws SQLException { |
561 | try { |
562 | resultSetHoldability = statementHoldabilityCheck(resultSetHoldability); |
563 | |
564 | return control.wrapStatement( |
565 | getRealConnection().prepareStatement(sql, resultSetType, |
566 | resultSetConcurrency, resultSetHoldability), sql, null); |
567 | } |
568 | catch (SQLException se) |
569 | { |
570 | notifyException(se); |
571 | throw se; |
572 | } |
573 | } |
574 | |
575 | /** |
576 | * Get the holdability for statements created by this connection |
577 | * when holdability is not passed in. |
578 | */ |
579 | public final int getHoldability() throws SQLException { |
580 | try { |
581 | return getRealConnection().getHoldability(); |
582 | } |
583 | catch (SQLException se) |
584 | { |
585 | notifyException(se); |
586 | throw se; |
587 | } |
588 | } |
589 | |
590 | /* |
591 | ** Methods private to the class. |
592 | */ |
593 | |
594 | /** |
595 | * Check the result set holdability when creating a statement |
596 | * object. Section 16.1.3.1 of JDBC 4.0 (proposed final draft) |
597 | * says the driver may change the holdabilty and add a SQLWarning |
598 | * to the Connection object. |
599 | * |
600 | * This work-in-progress implementation throws an exception |
601 | * to match the old behaviour just as part of incremental development. |
602 | */ |
603 | final int statementHoldabilityCheck(int resultSetHoldability) |
604 | throws SQLException |
605 | { |
606 | int holdability = control.checkHoldCursors(resultSetHoldability, true); |
607 | if (holdability != resultSetHoldability) { |
608 | SQLWarning w = |
609 | EmbedSQLWarning.newEmbedSQLWarning(SQLState.HOLDABLE_RESULT_SET_NOT_AVAILABLE); |
610 | |
611 | addWarning(w); |
612 | } |
613 | |
614 | return holdability; |
615 | |
616 | } |
617 | } |