KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > cpdsadapter > ConnectionImpl


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.cpdsadapter;
18
19 import java.util.Map JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.CallableStatement JavaDoc;
24 import java.sql.Statement JavaDoc;
25 import java.sql.SQLWarning JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 /**
29  * This class is the <code>Connection</code> that will be returned
30  * from <code>PooledConnectionImpl.getConnection()</code>.
31  * Most methods are wrappers around the jdbc 1.x <code>Connection</code>.
32  * A few exceptions include preparedStatement, close and isClosed.
33  * In accordance with the jdbc specification this Connection cannot
34  * be used after closed() is called. Any further usage will result in an
35  * SQLException.
36  *
37  * @author John D. McNally
38  * @version $Revision: 1.9 $ $Date: 2004/02/28 12:18:17 $
39  */

40 class ConnectionImpl implements Connection JavaDoc {
41     private static final String JavaDoc CLOSED
42             = "Attempted to use Connection after closed() was called.";
43
44     /** The JDBC database connection. */
45     private Connection JavaDoc connection;
46
47     /** The object that instantiated this object */
48      private PooledConnectionImpl pooledConnection;
49
50     /** Marks whether is Connection is still usable. */
51     boolean isClosed;
52
53     /**
54      * Creates a <code>ConnectionImpl</code>.
55      *
56      * @param pooledConnection The PooledConnection that is calling the ctor.
57      * @param connection The JDBC 1.x Connection to wrap.
58      */

59     ConnectionImpl(PooledConnectionImpl pooledConnection,
60             Connection JavaDoc connection) {
61         this.pooledConnection = pooledConnection;
62         this.connection = connection;
63         isClosed = false;
64     }
65
66     /**
67      * The finalizer helps prevent <code>ConnectionPool</code> leakage.
68      */

69     protected void finalize() throws Throwable JavaDoc {
70         if (!isClosed) {
71             // If this DBConnection object is finalized while linked
72
// to a ConnectionPool, it means that it was taken from a pool
73
// and not returned. We log this fact, close the underlying
74
// Connection, and return it to the ConnectionPool.
75
throw new SQLException JavaDoc("A ConnectionImpl was finalized "
76                       + "without being closed which will cause leakage of "
77                       + " PooledConnections from the ConnectionPool.");
78         }
79     }
80
81     /**
82      * Throws an SQLException, if isClosed() is true
83      */

84     private void assertOpen() throws SQLException JavaDoc {
85         if (isClosed) {
86             throw new SQLException JavaDoc(CLOSED);
87         }
88     }
89
90     // ***********************************************************************
91
// java.sql.Connection implementation using wrapped Connection
92
// ***********************************************************************
93

94     /**
95      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
96      *
97      * @exception SQLException if this connection is closed or an error occurs
98      * in the wrapped connection.
99      */

100     public void clearWarnings() throws SQLException JavaDoc {
101         assertOpen();
102         connection.clearWarnings();
103     }
104
105     /**
106      * Marks the Connection as closed, and notifies the pool that the
107      * pooled connection is available.
108      * In accordance with the jdbc specification this Connection cannot
109      * be used after closed() is called. Any further usage will result in an
110      * SQLException.
111      *
112      * @exception SQLException The database connection couldn't be closed.
113      */

114     public void close() throws SQLException JavaDoc {
115         assertOpen();
116         isClosed = true;
117         pooledConnection.notifyListeners();
118     }
119
120     /**
121      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
122      *
123      * @exception SQLException if this connection is closed or an error occurs
124      * in the wrapped connection.
125      */

126     public void commit() throws SQLException JavaDoc {
127         assertOpen();
128         connection.commit();
129     }
130
131     /**
132      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
133      *
134      * @exception SQLException if this connection is closed or an error occurs
135      * in the wrapped connection.
136      */

137     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
138         assertOpen();
139         return connection.createStatement();
140     }
141
142     /**
143      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
144      *
145      * @exception SQLException if this connection is closed or an error occurs
146      * in the wrapped connection.
147      */

148     public Statement JavaDoc createStatement(int resultSetType,
149                                      int resultSetConcurrency)
150             throws SQLException JavaDoc {
151         assertOpen();
152         return connection
153                 .createStatement(resultSetType, resultSetConcurrency);
154     }
155
156     /**
157      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
158      *
159      * @exception SQLException if this connection is closed or an error occurs
160      * in the wrapped connection.
161      */

162     public boolean getAutoCommit() throws SQLException JavaDoc {
163         assertOpen();
164         return connection.getAutoCommit();
165     }
166
167     /**
168      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
169      *
170      * @exception SQLException if this connection is closed or an error occurs
171      * in the wrapped connection.
172      */

173     public String JavaDoc getCatalog() throws SQLException JavaDoc {
174         assertOpen();
175         return connection.getCatalog();
176     }
177
178     /**
179      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
180      *
181      * @exception SQLException if this connection is closed or an error occurs
182      * in the wrapped connection.
183      */

184     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
185         assertOpen();
186         return connection.getMetaData();
187     }
188
189     /**
190      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
191      *
192      * @exception SQLException if this connection is closed or an error occurs
193      * in the wrapped connection.
194      */

195     public int getTransactionIsolation() throws SQLException JavaDoc {
196         assertOpen();
197         return connection.getTransactionIsolation();
198     }
199
200     /**
201      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
202      *
203      * @exception SQLException if this connection is closed or an error occurs
204      * in the wrapped connection.
205      */

206     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
207         assertOpen();
208         return connection.getTypeMap();
209     }
210
211     /**
212      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
213      *
214      * @exception SQLException if this connection is closed or an error occurs
215      * in the wrapped connection.
216      */

217     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
218         assertOpen();
219         return connection.getWarnings();
220     }
221
222     /**
223      * Returns true after close() is called, and false prior to that.
224      *
225      * @return a <code>boolean</code> value
226      */

227     public boolean isClosed() {
228         return isClosed;
229     }
230
231     /**
232      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
233      *
234      * @exception SQLException if this connection is closed or an error occurs
235      * in the wrapped connection.
236      */

237     public boolean isReadOnly() throws SQLException JavaDoc {
238         assertOpen();
239         return connection.isReadOnly();
240     }
241
242     /**
243      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
244      *
245      * @exception SQLException if this connection is closed or an error occurs
246      * in the wrapped connection.
247      */

248     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
249         assertOpen();
250         return connection.nativeSQL(sql);
251     }
252
253     /**
254      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
255      *
256      * @exception SQLException if this connection is closed or an error occurs
257      * in the wrapped connection.
258      */

259     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
260         assertOpen();
261         return connection.prepareCall(sql);
262     }
263
264     /**
265      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
266      *
267      * @exception SQLException if this connection is closed or an error occurs
268      * in the wrapped connection.
269      */

270     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
271                                          int resultSetConcurrency)
272             throws SQLException JavaDoc {
273         assertOpen();
274         return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
275     }
276
277     /**
278      * If pooling of <code>PreparedStatement</code>s is turned on in the
279      * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
280      * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
281      *
282      * @exception SQLException if this connection is closed or an error occurs
283      * in the wrapped connection.
284      */

285     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
286         assertOpen();
287         return pooledConnection.prepareStatement(sql);
288     }
289
290     /**
291      * If pooling of <code>PreparedStatement</code>s is turned on in the
292      * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
293      * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
294      *
295      * @exception SQLException if this connection is closed or an error occurs
296      * in the wrapped connection.
297      */

298     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
299                                               int resultSetConcurrency)
300             throws SQLException JavaDoc {
301         assertOpen();
302         return pooledConnection
303             .prepareStatement(sql, resultSetType, resultSetConcurrency);
304     }
305
306     /**
307      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
308      *
309      * @exception SQLException if this connection is closed or an error occurs
310      * in the wrapped connection.
311      */

312     public void rollback() throws SQLException JavaDoc {
313         assertOpen();
314         connection.rollback();
315     }
316
317     /**
318      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
319      *
320      * @exception SQLException if this connection is closed or an error occurs
321      * in the wrapped connection.
322      */

323     public void setAutoCommit(boolean b) throws SQLException JavaDoc {
324         assertOpen();
325         connection.setAutoCommit(b);
326     }
327
328     /**
329      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
330      *
331      * @exception SQLException if this connection is closed or an error occurs
332      * in the wrapped connection.
333      */

334     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
335         assertOpen();
336         connection.setCatalog(catalog);
337     }
338
339     /**
340      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
341      *
342      * @exception SQLException if this connection is closed or an error occurs
343      * in the wrapped connection.
344      */

345     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
346         assertOpen();
347         connection.setReadOnly(readOnly);
348     }
349
350     /**
351      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
352      *
353      * @exception SQLException if this connection is closed or an error occurs
354      * in the wrapped connection.
355      */

356     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
357         assertOpen();
358         connection.setTransactionIsolation(level);
359     }
360
361     /**
362      * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
363      *
364      * @exception SQLException if this connection is closed or an error occurs
365      * in the wrapped connection.
366      */

367     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
368         assertOpen();
369         connection.setTypeMap(map);
370     }
371
372     // ------------------- JDBC 3.0 -----------------------------------------
373
// Will be commented by the build process on a JDBC 2.0 system
374

375 /* JDBC_3_ANT_KEY_BEGIN */
376
377     public int getHoldability() throws SQLException JavaDoc {
378         assertOpen();
379         return connection.getHoldability();
380     }
381
382     public void setHoldability(int holdability) throws SQLException JavaDoc {
383         assertOpen();
384         connection.setHoldability(holdability);
385     }
386
387     public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
388         assertOpen();
389         return connection.setSavepoint();
390     }
391
392     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
393         assertOpen();
394         return connection.setSavepoint(name);
395     }
396
397     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
398         assertOpen();
399         connection.rollback(savepoint);
400     }
401
402     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint)
403             throws SQLException JavaDoc {
404         assertOpen();
405         connection.releaseSavepoint(savepoint);
406     }
407
408     public Statement JavaDoc createStatement(int resultSetType,
409                                      int resultSetConcurrency,
410                                      int resultSetHoldability)
411             throws SQLException JavaDoc {
412         assertOpen();
413         return connection.createStatement(resultSetType, resultSetConcurrency,
414                                      resultSetHoldability);
415     }
416
417     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
418                                               int resultSetConcurrency,
419                                               int resultSetHoldability)
420             throws SQLException JavaDoc {
421         assertOpen();
422         return connection.prepareStatement(sql, resultSetType,
423                                       resultSetConcurrency,
424                                       resultSetHoldability);
425     }
426
427     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
428                                          int resultSetConcurrency,
429                                          int resultSetHoldability)
430             throws SQLException JavaDoc {
431         assertOpen();
432         return connection.prepareCall(sql, resultSetType,
433                                  resultSetConcurrency,
434                                  resultSetHoldability);
435     }
436
437     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys)
438             throws SQLException JavaDoc {
439         assertOpen();
440         return connection.prepareStatement(sql, autoGeneratedKeys);
441     }
442
443     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int columnIndexes[])
444             throws SQLException JavaDoc {
445         assertOpen();
446         return connection.prepareStatement(sql, columnIndexes);
447     }
448
449     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc columnNames[])
450             throws SQLException JavaDoc {
451         assertOpen();
452         return connection.prepareStatement(sql, columnNames);
453     }
454
455 /* JDBC_3_ANT_KEY_END */
456 }
457
Popular Tags