KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > jdbc > BrokeredConnection


1 /*
2
3    Derby - Class org.apache.derby.iapi.jdbc.BrokeredConnection
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.jdbc;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.CallableStatement JavaDoc;
28 import java.sql.DatabaseMetaData JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.SQLWarning JavaDoc;
31
32 import org.apache.derby.impl.jdbc.EmbedSQLWarning;
33 import org.apache.derby.impl.jdbc.Util;
34
35 import java.io.ObjectOutput JavaDoc;
36 import java.io.ObjectInput JavaDoc;
37
38 import java.lang.reflect.*;
39
40 import org.apache.derby.iapi.reference.JDBC30Translation;
41 import org.apache.derby.iapi.error.PublicAPI;
42 import org.apache.derby.iapi.error.StandardException;
43 import org.apache.derby.shared.common.reference.SQLState;
44
45 /**
46  * This is a rudimentary connection that delegates
47  * EVERYTHING to Connection.
48  */

49 public class BrokeredConnection implements EngineConnection
50 {
51     
52     // default for Derby
53
int stateHoldability = JDBC30Translation.HOLD_CURSORS_OVER_COMMIT;
54
55     final BrokeredConnectionControl control;
56     private boolean isClosed;
57         private String JavaDoc connString;
58
59     /**
60         Maintain state as seen by this Connection handle, not the state
61         of the underlying Connection it is attached to.
62     */

63     private int stateIsolationLevel;
64     private boolean stateReadOnly;
65     private boolean stateAutoCommit;
66
67     /////////////////////////////////////////////////////////////////////////
68
//
69
// CONSTRUCTORS
70
//
71
/////////////////////////////////////////////////////////////////////////
72

73     public BrokeredConnection(BrokeredConnectionControl control)
74     {
75         this.control = control;
76     }
77
78     public final void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc
79     {
80         try {
81             control.checkAutoCommit(autoCommit);
82
83             getRealConnection().setAutoCommit(autoCommit);
84
85             stateAutoCommit = autoCommit;
86         } catch (SQLException JavaDoc sqle) {
87             notifyException(sqle);
88             throw sqle;
89         }
90     }
91     public final boolean getAutoCommit() throws SQLException JavaDoc
92     {
93         try {
94             return getRealConnection().getAutoCommit();
95         } catch (SQLException JavaDoc sqle) {
96             notifyException(sqle);
97             throw sqle;
98         }
99     }
100     public final Statement JavaDoc createStatement() throws SQLException JavaDoc
101     {
102         try {
103             return control.wrapStatement(getRealConnection().createStatement());
104         } catch (SQLException JavaDoc sqle) {
105             notifyException(sqle);
106             throw sqle;
107         }
108     }
109
110     public final PreparedStatement JavaDoc prepareStatement(String JavaDoc sql)
111         throws SQLException JavaDoc
112     {
113         try {
114             return control.wrapStatement(getRealConnection().prepareStatement(sql), sql, null);
115         } catch (SQLException JavaDoc sqle) {
116             notifyException(sqle);
117             throw sqle;
118         }
119     }
120
121     public final CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc
122     {
123         try {
124             return control.wrapStatement(getRealConnection().prepareCall(sql), sql);
125         } catch (SQLException JavaDoc sqle) {
126             notifyException(sqle);
127             throw sqle;
128         }
129     }
130
131     public final String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
132     {
133         try {
134             return getRealConnection().nativeSQL(sql);
135         } catch (SQLException JavaDoc sqle) {
136             notifyException(sqle);
137             throw sqle;
138         }
139     }
140
141     public final void commit() throws SQLException JavaDoc
142     {
143         try {
144             control.checkCommit();
145             getRealConnection().commit();
146         } catch (SQLException JavaDoc sqle) {
147             notifyException(sqle);
148             throw sqle;
149         }
150     }
151
152     public final void rollback() throws SQLException JavaDoc
153     {
154         try {
155             control.checkRollback();
156             getRealConnection().rollback();
157         } catch (SQLException JavaDoc sqle) {
158             notifyException(sqle);
159             throw sqle;
160         }
161     }
162
163     public final void close() throws SQLException JavaDoc
164     {
165         if (isClosed)
166             return;
167
168         try {
169             if (!control.closingConnection()) {
170                 isClosed = true;
171                 return;
172             }
173
174             isClosed = true;
175
176
177             getRealConnection().close();
178         } catch (SQLException JavaDoc sqle) {
179             notifyException(sqle);
180             throw sqle;
181         }
182     }
183
184     public final boolean isClosed() throws SQLException JavaDoc
185     {
186         if (isClosed)
187             return true;
188         try {
189             boolean realIsClosed = getRealConnection().isClosed();
190             if (realIsClosed) {
191                 control.closingConnection();
192                 isClosed = true;
193             }
194             return realIsClosed;
195         } catch (SQLException JavaDoc sqle) {
196             notifyException(sqle);
197             throw sqle;
198         }
199     }
200
201     public final SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
202     {
203         try {
204             return getRealConnection().getWarnings();
205         } catch (SQLException JavaDoc sqle) {
206             notifyException(sqle);
207             throw sqle;
208         }
209     }
210
211     public final void clearWarnings() throws SQLException JavaDoc
212     {
213         try {
214             getRealConnection().clearWarnings();
215         } catch (SQLException JavaDoc sqle) {
216             notifyException(sqle);
217             throw sqle;
218         }
219     }
220
221     public final DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
222     {
223         try {
224             return getRealConnection().getMetaData();
225         } catch (SQLException JavaDoc sqle) {
226             notifyException(sqle);
227             throw sqle;
228         }
229     }
230
231     public final void setReadOnly(boolean readOnly) throws SQLException JavaDoc
232     {
233         try {
234             getRealConnection().setReadOnly(readOnly);
235             stateReadOnly = readOnly;
236         } catch (SQLException JavaDoc sqle) {
237             notifyException(sqle);
238             throw sqle;
239         }
240     }
241
242     public final boolean isReadOnly() throws SQLException JavaDoc
243     {
244         try {
245             return getRealConnection().isReadOnly();
246         } catch (SQLException JavaDoc sqle) {
247             notifyException(sqle);
248             throw sqle;
249         }
250     }
251
252     public final void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc
253     {
254         try {
255             getRealConnection().setCatalog(catalog);
256         } catch (SQLException JavaDoc sqle) {
257             notifyException(sqle);
258             throw sqle;
259         }
260     }
261
262     public final String JavaDoc getCatalog() throws SQLException JavaDoc
263     {
264         try {
265             return getRealConnection().getCatalog();
266         } catch (SQLException JavaDoc sqle) {
267             notifyException(sqle);
268             throw sqle;
269         }
270     }
271
272     public final void setTransactionIsolation(int level) throws SQLException JavaDoc
273     {
274         try {
275             getRealConnection().setTransactionIsolation(level);
276             stateIsolationLevel = level;
277         } catch (SQLException JavaDoc sqle) {
278             notifyException(sqle);
279             throw sqle;
280         }
281     }
282
283     public final int getTransactionIsolation() throws SQLException JavaDoc
284     {
285         try {
286             return getRealConnection().getTransactionIsolation();
287         } catch (SQLException JavaDoc sqle) {
288             notifyException(sqle);
289             throw sqle;
290         }
291     }
292
293     public final Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency)
294       throws SQLException JavaDoc
295     {
296         try
297         {
298             return control.wrapStatement(getRealConnection().
299                 createStatement(resultSetType, resultSetConcurrency));
300         }
301         catch (SQLException JavaDoc se)
302         {
303             notifyException(se);
304             throw se;
305         }
306     }
307
308
309     public final PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
310                     int resultSetConcurrency)
311        throws SQLException JavaDoc
312     {
313         try
314         {
315             return control.wrapStatement(getRealConnection().
316                 prepareStatement(sql, resultSetType, resultSetConcurrency), sql, null);
317         }
318         catch (SQLException JavaDoc se)
319         {
320             notifyException(se);
321             throw se;
322         }
323     }
324
325     public final CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
326                  int resultSetConcurrency) throws SQLException JavaDoc
327     {
328         try
329         {
330             return control.wrapStatement(getRealConnection().
331                 prepareCall(sql, resultSetType, resultSetConcurrency), sql);
332         }
333         catch (SQLException JavaDoc se)
334         {
335             notifyException(se);
336             throw se;
337         }
338     }
339
340     public java.util.Map JavaDoc getTypeMap() throws SQLException JavaDoc
341     {
342         try
343         {
344             return getRealConnection().getTypeMap();
345         }
346         catch (SQLException JavaDoc se)
347         {
348             notifyException(se);
349             throw se;
350         }
351     }
352
353     public final void setTypeMap(java.util.Map JavaDoc map) throws SQLException JavaDoc
354     {
355         try
356         {
357             getRealConnection().setTypeMap(map);
358         }
359         catch (SQLException JavaDoc se)
360         {
361             notifyException(se);
362             throw se;
363         }
364     }
365
366     /////////////////////////////////////////////////////////////////////////
367
//
368
// MINIONS
369
//
370
/////////////////////////////////////////////////////////////////////////
371

372     /**
373       * A little indirection for getting the real connection.
374       *
375       * @return the current connection
376       */

377     final EngineConnection getRealConnection() throws SQLException JavaDoc {
378         if (isClosed)
379             throw Util.noCurrentConnection();
380
381         return control.getRealConnection();
382     }
383
384     final void notifyException(SQLException JavaDoc sqle) {
385         if (!isClosed)
386             control.notifyException(sqle);
387     }
388
389     /**
390         Sync up the state of the underlying connection
391         with the state of this new handle.
392     */

393     public void syncState() throws SQLException JavaDoc {
394         EngineConnection conn = getRealConnection();
395
396         stateIsolationLevel = conn.getTransactionIsolation();
397         stateReadOnly = conn.isReadOnly();
398         stateAutoCommit = conn.getAutoCommit();
399         stateHoldability = conn.getHoldability();
400     }
401
402     /**
403         Isolation level state in BrokeredConnection can get out of sync
404         if the isolation is set using SQL rather than JDBC. In order to
405         ensure correct state level information, this method is called
406         at the start and end of a global transaction.
407     */

408     public void getIsolationUptoDate() throws SQLException JavaDoc {
409         if (control.isIsolationLevelSetUsingSQLorJDBC()) {
410             stateIsolationLevel = getRealConnection().getTransactionIsolation();
411             control.resetIsolationLevelFlag();
412         }
413     }
414     /**
415         Set the state of the underlying connection according to the
416         state of this connection's view of state.
417
418         @param complete If true set the complete state of the underlying
419         Connection, otherwise set only the Connection related state (ie.
420         the non-transaction specific state).
421
422
423     */

424     public void setState(boolean complete) throws SQLException JavaDoc {
425         Class JavaDoc[] CONN_PARAM = { Integer.TYPE };
426         Object JavaDoc[] CONN_ARG = { new Integer JavaDoc(stateHoldability)};
427
428         Connection JavaDoc conn = getRealConnection();
429
430         if (complete) {
431             conn.setTransactionIsolation(stateIsolationLevel);
432             conn.setReadOnly(stateReadOnly);
433             conn.setAutoCommit(stateAutoCommit);
434             // make the underlying connection pick my holdability state
435
// since holdability is a state of the connection handle
436
// not the underlying transaction.
437
// jdk13 does not have Connection.setHoldability method and hence using
438
// reflection to cover both jdk13 and higher jdks
439
try {
440                 Method sh = conn.getClass().getMethod("setHoldability", CONN_PARAM);
441                 sh.invoke(conn, CONN_ARG);
442             } catch( Exception JavaDoc e)
443             {
444                 throw PublicAPI.wrapStandardException( StandardException.plainWrapException( e));
445             }
446         }
447     }
448
449     public BrokeredStatement newBrokeredStatement(BrokeredStatementControl statementControl) throws SQLException JavaDoc {
450         return new BrokeredStatement(statementControl, getJDBCLevel());
451     }
452     public BrokeredPreparedStatement newBrokeredStatement(BrokeredStatementControl statementControl, String JavaDoc sql, Object JavaDoc generatedKeys) throws SQLException JavaDoc {
453         return new BrokeredPreparedStatement(statementControl, getJDBCLevel(), sql);
454     }
455     public BrokeredCallableStatement newBrokeredStatement(BrokeredStatementControl statementControl, String JavaDoc sql) throws SQLException JavaDoc {
456         return new BrokeredCallableStatement(statementControl, getJDBCLevel(), sql);
457     }
458
459     /**
460      * set the DrdaId for this connection. The drdaID prints with the
461      * statement text to the errror log
462      * @param drdaID drdaID to be used for this connection
463      *
464      */

465     public final void setDrdaID(String JavaDoc drdaID)
466     {
467         try {
468             getRealConnection().setDrdaID(drdaID);
469         } catch (SQLException JavaDoc sqle)
470         {
471             // connection is closed, just ignore drdaId
472
// since connection cannot be used.
473
}
474     }
475
476     /**
477      * Set the internal isolation level to use for preparing statements.
478      * Subsequent prepares will use this isoalation level
479      * @param level - internal isolation level
480      * @throws SQLException
481      * See EmbedConnection#setPrepareIsolation
482      *
483      */

484     public final void setPrepareIsolation(int level) throws SQLException JavaDoc
485     {
486         getRealConnection().setPrepareIsolation(level);
487     }
488
489     /**
490      * get the isolation level that is currently being used to prepare
491      * statements (used for network server)
492      *
493      * @throws SQLException
494      * @return current prepare isolation level
495      * See EmbedConnection#getPrepareIsolation
496      */

497     public final int getPrepareIsolation() throws SQLException JavaDoc
498     {
499         return getRealConnection().getPrepareIsolation();
500     }
501     
502     /**
503      * Add a SQLWarning to this Connection object.
504      * @throws SQLException
505      */

506     public final void addWarning(SQLWarning JavaDoc w) throws SQLException JavaDoc
507     {
508         getRealConnection().addWarning(w);
509     }
510             
511     /**
512      * Checks if the connection is closed and throws an exception if
513      * it is.
514      *
515      * @exception SQLException if the connection is closed
516      */

517     protected final void checkIfClosed() throws SQLException JavaDoc {
518         if (isClosed()) {
519             throw Util.noCurrentConnection();
520         }
521     }
522
523     /**
524      * Get the string representation for this connection. Return
525      * the class name/hash code and various debug information.
526      *
527      * @return unique string representation for this connection
528      */

529     public String JavaDoc toString()
530     {
531         if ( connString == null )
532         {
533             String JavaDoc wrappedString;
534             try
535             {
536                 wrappedString = getRealConnection().toString();
537             }
538             catch ( SQLException JavaDoc e )
539             {
540                 wrappedString = "<none>";
541             }
542             
543             connString = this.getClass().getName() + "@" + this.hashCode() +
544                 ", Wrapped Connection = " + wrappedString;
545         }
546         
547         return connString;
548     }
549
550     int getJDBCLevel() { return 2;}
551
552     /*
553      * JDBC 3.0 methods that are exposed through EngineConnection.
554      */

555     
556     /**
557      * Prepare statement with explicit holdability.
558      */

559     public final PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
560             int resultSetType, int resultSetConcurrency,
561             int resultSetHoldability) throws SQLException JavaDoc {
562         try {
563             resultSetHoldability = statementHoldabilityCheck(resultSetHoldability);
564             
565             return control.wrapStatement(
566                 getRealConnection().prepareStatement(sql, resultSetType,
567                         resultSetConcurrency, resultSetHoldability), sql, null);
568         }
569         catch (SQLException JavaDoc se)
570         {
571             notifyException(se);
572             throw se;
573         }
574     }
575
576     /**
577      * Get the holdability for statements created by this connection
578      * when holdability is not passed in.
579      */

580     public final int getHoldability() throws SQLException JavaDoc {
581         try {
582             return getRealConnection().getHoldability();
583         }
584         catch (SQLException JavaDoc se)
585         {
586             notifyException(se);
587             throw se;
588         }
589     }
590     
591     /*
592     ** Methods private to the class.
593     */

594     
595     /**
596      * Check the result set holdability when creating a statement
597      * object. Section 16.1.3.1 of JDBC 4.0 (proposed final draft)
598      * says the driver may change the holdabilty and add a SQLWarning
599      * to the Connection object.
600      *
601      * This work-in-progress implementation throws an exception
602      * to match the old behaviour just as part of incremental development.
603      */

604     final int statementHoldabilityCheck(int resultSetHoldability)
605         throws SQLException JavaDoc
606     {
607         int holdability = control.checkHoldCursors(resultSetHoldability, true);
608         if (holdability != resultSetHoldability) {
609             SQLWarning JavaDoc w =
610                  EmbedSQLWarning.newEmbedSQLWarning(SQLState.HOLDABLE_RESULT_SET_NOT_AVAILABLE);
611             
612             addWarning(w);
613         }
614         
615         return holdability;
616         
617     }
618 }
619
Popular Tags