KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > DelegatingConnection


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.commons.dbcp;
18
19 import java.sql.CallableStatement JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.SQLWarning JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 /**
30  * A base delegating implementation of {@link Connection}.
31  * <p>
32  * All of the methods from the {@link Connection} interface
33  * simply check to see that the {@link Connection} is active,
34  * and call the corresponding method on the "delegate"
35  * provided in my constructor.
36  * <p>
37  * Extends AbandonedTrace to implement Connection tracking and
38  * logging of code which created the Connection. Tracking the
39  * Connection ensures that the AbandonedObjectPool can close
40  * this connection and recycle it if its pool of connections
41  * is nearing exhaustion and this connection's last usage is
42  * older than the removeAbandonedTimeout.
43  *
44  * @author Rodney Waldhoff
45  * @author Glenn L. Nielsen
46  * @author James House
47  * @author Dirk Verbeeck
48  * @version $Revision: 1.19 $ $Date: 2004/03/06 13:35:31 $
49  */

50 public class DelegatingConnection extends AbandonedTrace
51         implements Connection JavaDoc {
52     /** My delegate {@link Connection}. */
53     protected Connection JavaDoc _conn = null;
54
55     protected boolean _closed = false;
56     
57     /**
58      * Create a wrapper for the Connectin which traces this
59      * Connection in the AbandonedObjectPool.
60      *
61      * @param c the {@link Connection} to delegate all calls to.
62      */

63     public DelegatingConnection(Connection JavaDoc c) {
64         super();
65         _conn = c;
66     }
67
68     /**
69      * Create a wrapper for the Connection which traces
70      * the Statements created so that any unclosed Statements
71      * can be closed when this Connection is closed.
72      *
73      * @param Connection the {@link Connection} to delegate all calls to.
74      * @param AbandonedConfig the configuration for tracing abandoned objects
75      * @deprecated AbandonedConfig is now deprecated.
76      */

77     public DelegatingConnection(Connection JavaDoc c, AbandonedConfig config) {
78         super(config);
79         _conn = c;
80     }
81
82     /**
83      * Returns my underlying {@link Connection}.
84      * @return my underlying {@link Connection}.
85      */

86     public Connection JavaDoc getDelegate() {
87         return _conn;
88     }
89
90     public boolean equals(Object JavaDoc obj) {
91         Connection JavaDoc delegate = getInnermostDelegate();
92         if (delegate == null) {
93             return false;
94         }
95         if (obj instanceof DelegatingConnection) {
96             DelegatingConnection c = (DelegatingConnection) obj;
97             return delegate.equals(c.getInnermostDelegate());
98         }
99         else {
100             return delegate.equals(obj);
101         }
102     }
103
104     public int hashCode() {
105         Object JavaDoc obj = getInnermostDelegate();
106         if (obj == null) {
107             return 0;
108         }
109         return obj.hashCode();
110     }
111
112
113     /**
114      * If my underlying {@link Connection} is not a
115      * <tt>DelegatingConnection</tt>, returns it,
116      * otherwise recursively invokes this method on
117      * my delegate.
118      * <p>
119      * Hence this method will return the first
120      * delegate that is not a <tt>DelegatingConnection</tt>,
121      * or <tt>null</tt> when no non-<tt>DelegatingConnection</tt>
122      * delegate can be found by transversing this chain.
123      * <p>
124      * This method is useful when you may have nested
125      * <tt>DelegatingConnection</tt>s, and you want to make
126      * sure to obtain a "genuine" {@link Connection}.
127      */

128     public Connection JavaDoc getInnermostDelegate() {
129         Connection JavaDoc c = _conn;
130         while(c != null && c instanceof DelegatingConnection) {
131             c = ((DelegatingConnection)c).getDelegate();
132             if(this == c) {
133                 return null;
134             }
135         }
136         return c;
137     }
138
139     /** Sets my delegate. */
140     public void setDelegate(Connection JavaDoc c) {
141         _conn = c;
142     }
143
144     /**
145      * Closes the underlying connection, and close
146      * any Statements that were not explicitly closed.
147      */

148     public void close() throws SQLException JavaDoc
149     {
150         passivate();
151         _conn.close();
152     }
153
154     protected void handleException(SQLException JavaDoc e) throws SQLException JavaDoc {
155         throw e;
156     }
157
158     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
159         checkOpen();
160         try {
161             return new DelegatingStatement(this, _conn.createStatement());
162         }
163         catch (SQLException JavaDoc e) {
164             handleException(e);
165             return null;
166         }
167     }
168
169     public Statement JavaDoc createStatement(int resultSetType,
170                                      int resultSetConcurrency) throws SQLException JavaDoc {
171         checkOpen();
172         try {
173             return new DelegatingStatement
174                 (this, _conn.createStatement(resultSetType,resultSetConcurrency));
175         }
176         catch (SQLException JavaDoc e) {
177             handleException(e);
178             return null;
179         }
180     }
181
182     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
183         checkOpen();
184         try {
185             return new DelegatingPreparedStatement
186                 (this, _conn.prepareStatement(sql));
187         }
188         catch (SQLException JavaDoc e) {
189             handleException(e);
190             return null;
191         }
192     }
193
194     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql,
195                                               int resultSetType,
196                                               int resultSetConcurrency) throws SQLException JavaDoc {
197         checkOpen();
198         try {
199             return new DelegatingPreparedStatement
200                 (this, _conn.prepareStatement
201                     (sql,resultSetType,resultSetConcurrency));
202         }
203         catch (SQLException JavaDoc e) {
204             handleException(e);
205             return null;
206         }
207     }
208
209     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
210         checkOpen();
211         try {
212             return new DelegatingCallableStatement(this, _conn.prepareCall(sql));
213         }
214         catch (SQLException JavaDoc e) {
215             handleException(e);
216             return null;
217         }
218     }
219
220     public CallableStatement JavaDoc prepareCall(String JavaDoc sql,
221                                          int resultSetType,
222                                          int resultSetConcurrency) throws SQLException JavaDoc {
223         checkOpen();
224         try {
225             return new DelegatingCallableStatement
226                 (this, _conn.prepareCall(sql, resultSetType,resultSetConcurrency));
227         }
228         catch (SQLException JavaDoc e) {
229             handleException(e);
230             return null;
231         }
232     }
233
234     public void clearWarnings() throws SQLException JavaDoc
235     { checkOpen(); try { _conn.clearWarnings(); } catch (SQLException JavaDoc e) { handleException(e); } }
236     
237     public void commit() throws SQLException JavaDoc
238     { checkOpen(); try { _conn.commit(); } catch (SQLException JavaDoc e) { handleException(e); } }
239     
240     public boolean getAutoCommit() throws SQLException JavaDoc
241     { checkOpen(); try { return _conn.getAutoCommit(); } catch (SQLException JavaDoc e) { handleException(e); return false; }
242     }
243     public String JavaDoc getCatalog() throws SQLException JavaDoc
244     { checkOpen(); try { return _conn.getCatalog(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
245     
246     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc
247     { checkOpen(); try { return _conn.getMetaData(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
248     
249     public int getTransactionIsolation() throws SQLException JavaDoc
250     { checkOpen(); try { return _conn.getTransactionIsolation(); } catch (SQLException JavaDoc e) { handleException(e); return -1; } }
251     
252     public Map JavaDoc getTypeMap() throws SQLException JavaDoc
253     { checkOpen(); try { return _conn.getTypeMap(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
254     
255     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc
256     { checkOpen(); try { return _conn.getWarnings(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
257     
258     public boolean isReadOnly() throws SQLException JavaDoc
259     { checkOpen(); try { return _conn.isReadOnly(); } catch (SQLException JavaDoc e) { handleException(e); return false; } }
260     
261     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc
262     { checkOpen(); try { return _conn.nativeSQL(sql); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
263     
264     public void rollback() throws SQLException JavaDoc
265     { checkOpen(); try { _conn.rollback(); } catch (SQLException JavaDoc e) { handleException(e); } }
266     
267     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc
268     { checkOpen(); try { _conn.setAutoCommit(autoCommit); } catch (SQLException JavaDoc e) { handleException(e); } }
269
270     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc
271     { checkOpen(); try { _conn.setCatalog(catalog); } catch (SQLException JavaDoc e) { handleException(e); } }
272
273     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc
274     { checkOpen(); try { _conn.setReadOnly(readOnly); } catch (SQLException JavaDoc e) { handleException(e); } }
275
276     public void setTransactionIsolation(int level) throws SQLException JavaDoc
277     { checkOpen(); try { _conn.setTransactionIsolation(level); } catch (SQLException JavaDoc e) { handleException(e); } }
278
279     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc
280     { checkOpen(); try { _conn.setTypeMap(map); } catch (SQLException JavaDoc e) { handleException(e); } }
281
282     public boolean isClosed() throws SQLException JavaDoc {
283          if(_closed || _conn.isClosed()) {
284              return true;
285          }
286          return false;
287     }
288
289     protected void checkOpen() throws SQLException JavaDoc {
290         if(_closed) {
291             throw new SQLException JavaDoc("Connection is closed.");
292         }
293     }
294
295     protected void activate() {
296         _closed = false;
297         setLastUsed();
298         if(_conn instanceof DelegatingConnection) {
299             ((DelegatingConnection)_conn).activate();
300         }
301     }
302
303     protected void passivate() throws SQLException JavaDoc {
304         try {
305             // The JDBC spec requires that a Connection close any open
306
// Statement's when it is closed.
307
List JavaDoc statements = getTrace();
308             if( statements != null) {
309                 Statement JavaDoc[] set = new Statement JavaDoc[statements.size()];
310                 statements.toArray(set);
311                 for (int i = 0; i < set.length; i++) {
312                     set[i].close();
313                 }
314                 clearTrace();
315             }
316             setLastUsed(0);
317             if(_conn instanceof DelegatingConnection) {
318                 ((DelegatingConnection)_conn).passivate();
319             }
320         }
321         finally {
322             _closed = true;
323         }
324     }
325
326     // ------------------- JDBC 3.0 -----------------------------------------
327
// Will be commented by the build process on a JDBC 2.0 system
328

329 /* JDBC_3_ANT_KEY_BEGIN */
330
331     public int getHoldability() throws SQLException JavaDoc
332     { checkOpen(); try { return _conn.getHoldability(); } catch (SQLException JavaDoc e) { handleException(e); return 0; } }
333
334     public void setHoldability(int holdability) throws SQLException JavaDoc
335     { checkOpen(); try { _conn.setHoldability(holdability); } catch (SQLException JavaDoc e) { handleException(e); } }
336
337     public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc
338     { checkOpen(); try { return _conn.setSavepoint(); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
339
340     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc
341     { checkOpen(); try { return _conn.setSavepoint(name); } catch (SQLException JavaDoc e) { handleException(e); return null; } }
342
343     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc
344     { checkOpen(); try { _conn.rollback(savepoint); } catch (SQLException JavaDoc e) { handleException(e); } }
345
346     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc
347     { checkOpen(); try { _conn.releaseSavepoint(savepoint); } catch (SQLException JavaDoc e) { handleException(e); } }
348
349     public Statement JavaDoc createStatement(int resultSetType,
350                                      int resultSetConcurrency,
351                                      int resultSetHoldability) throws SQLException JavaDoc {
352         checkOpen();
353         try {
354             return new DelegatingStatement(this, _conn.createStatement(
355                 resultSetType, resultSetConcurrency, resultSetHoldability));
356         }
357         catch (SQLException JavaDoc e) {
358             handleException(e);
359             return null;
360         }
361     }
362
363     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
364                                               int resultSetConcurrency,
365                                               int resultSetHoldability) throws SQLException JavaDoc {
366         checkOpen();
367         try {
368             return new DelegatingPreparedStatement(this, _conn.prepareStatement(
369                 sql, resultSetType, resultSetConcurrency, resultSetHoldability));
370         }
371         catch (SQLException JavaDoc e) {
372             handleException(e);
373             return null;
374         }
375     }
376
377     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
378                                          int resultSetConcurrency,
379                                          int resultSetHoldability) throws SQLException JavaDoc {
380         checkOpen();
381         try {
382             return new DelegatingCallableStatement(this, _conn.prepareCall(
383                 sql, resultSetType, resultSetConcurrency, resultSetHoldability));
384         }
385         catch (SQLException JavaDoc e) {
386             handleException(e);
387             return null;
388         }
389     }
390
391     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
392         checkOpen();
393         try {
394             return new DelegatingPreparedStatement(this, _conn.prepareStatement(
395                 sql, autoGeneratedKeys));
396         }
397         catch (SQLException JavaDoc e) {
398             handleException(e);
399             return null;
400         }
401     }
402
403     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[]) throws SQLException JavaDoc {
404         checkOpen();
405         try {
406             return new DelegatingPreparedStatement(this, _conn.prepareStatement(
407                 sql, columnIndexes));
408         }
409         catch (SQLException JavaDoc e) {
410             handleException(e);
411             return null;
412         }
413     }
414
415     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException JavaDoc {
416         checkOpen();
417         try {
418             return new DelegatingPreparedStatement(this, _conn.prepareStatement(
419                 sql, columnNames));
420         }
421         catch (SQLException JavaDoc e) {
422             handleException(e);
423             return null;
424         }
425     }
426 /* JDBC_3_ANT_KEY_END */
427 }
428
Popular Tags