001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.dbcp.cpdsadapter;
019:
020: import java.util.Map;
021: import java.sql.Connection;
022: import java.sql.DatabaseMetaData;
023: import java.sql.PreparedStatement;
024: import java.sql.CallableStatement;
025: import java.sql.Statement;
026: import java.sql.SQLWarning;
027: import java.sql.SQLException;
028:
029: /**
030: * This class is the <code>Connection</code> that will be returned
031: * from <code>PooledConnectionImpl.getConnection()</code>.
032: * Most methods are wrappers around the jdbc 1.x <code>Connection</code>.
033: * A few exceptions include preparedStatement, close and isClosed.
034: * In accordance with the jdbc specification this Connection cannot
035: * be used after closed() is called. Any further usage will result in an
036: * SQLException.
037: *
038: * @author John D. McNally
039: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
040: */
041: class ConnectionImpl implements Connection {
042: private static final String CLOSED = "Attempted to use Connection after closed() was called.";
043:
044: /** The JDBC database connection. */
045: private Connection connection;
046:
047: /** The object that instantiated this object */
048: private PooledConnectionImpl pooledConnection;
049:
050: /** Marks whether is Connection is still usable. */
051: boolean isClosed;
052:
053: /**
054: * Creates a <code>ConnectionImpl</code>.
055: *
056: * @param pooledConnection The PooledConnection that is calling the ctor.
057: * @param connection The JDBC 1.x Connection to wrap.
058: */
059: ConnectionImpl(PooledConnectionImpl pooledConnection,
060: Connection connection) {
061: this .pooledConnection = pooledConnection;
062: this .connection = connection;
063: isClosed = false;
064: }
065:
066: /**
067: * The finalizer helps prevent <code>ConnectionPool</code> leakage.
068: */
069: protected void finalize() throws Throwable {
070: if (!isClosed) {
071: // If this DBConnection object is finalized while linked
072: // to a ConnectionPool, it means that it was taken from a pool
073: // and not returned. We log this fact, close the underlying
074: // Connection, and return it to the ConnectionPool.
075: throw new SQLException(
076: "A ConnectionImpl was finalized "
077: + "without being closed which will cause leakage of "
078: + " PooledConnections from the ConnectionPool.");
079: }
080: }
081:
082: /**
083: * Throws an SQLException, if isClosed() is true
084: */
085: private void assertOpen() throws SQLException {
086: if (isClosed) {
087: throw new SQLException(CLOSED);
088: }
089: }
090:
091: // ***********************************************************************
092: // java.sql.Connection implementation using wrapped Connection
093: // ***********************************************************************
094:
095: /**
096: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
097: *
098: * @exception SQLException if this connection is closed or an error occurs
099: * in the wrapped connection.
100: */
101: public void clearWarnings() throws SQLException {
102: assertOpen();
103: connection.clearWarnings();
104: }
105:
106: /**
107: * Marks the Connection as closed, and notifies the pool that the
108: * pooled connection is available.
109: * In accordance with the jdbc specification this Connection cannot
110: * be used after closed() is called. Any further usage will result in an
111: * SQLException.
112: *
113: * @exception SQLException The database connection couldn't be closed.
114: */
115: public void close() throws SQLException {
116: assertOpen();
117: isClosed = true;
118: pooledConnection.notifyListeners();
119: }
120:
121: /**
122: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
123: *
124: * @exception SQLException if this connection is closed or an error occurs
125: * in the wrapped connection.
126: */
127: public void commit() throws SQLException {
128: assertOpen();
129: connection.commit();
130: }
131:
132: /**
133: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
134: *
135: * @exception SQLException if this connection is closed or an error occurs
136: * in the wrapped connection.
137: */
138: public Statement createStatement() throws SQLException {
139: assertOpen();
140: return connection.createStatement();
141: }
142:
143: /**
144: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
145: *
146: * @exception SQLException if this connection is closed or an error occurs
147: * in the wrapped connection.
148: */
149: public Statement createStatement(int resultSetType,
150: int resultSetConcurrency) throws SQLException {
151: assertOpen();
152: return connection.createStatement(resultSetType,
153: 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 {
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 getCatalog() throws SQLException {
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 getMetaData() throws SQLException {
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 {
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 getTypeMap() throws SQLException {
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 getWarnings() throws SQLException {
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 {
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 nativeSQL(String sql) throws SQLException {
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 prepareCall(String sql)
260: throws SQLException {
261: assertOpen();
262: return connection.prepareCall(sql);
263: }
264:
265: /**
266: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
267: *
268: * @exception SQLException if this connection is closed or an error occurs
269: * in the wrapped connection.
270: */
271: public CallableStatement prepareCall(String sql, int resultSetType,
272: int resultSetConcurrency) throws SQLException {
273: assertOpen();
274: return connection.prepareCall(sql, resultSetType,
275: resultSetConcurrency);
276: }
277:
278: /**
279: * If pooling of <code>PreparedStatement</code>s is turned on in the
280: * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
281: * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
282: *
283: * @exception SQLException if this connection is closed or an error occurs
284: * in the wrapped connection.
285: */
286: public PreparedStatement prepareStatement(String sql)
287: throws SQLException {
288: assertOpen();
289: return pooledConnection.prepareStatement(sql);
290: }
291:
292: /**
293: * If pooling of <code>PreparedStatement</code>s is turned on in the
294: * {@link DriverAdapterCPDS}, a pooled object may be returned, otherwise
295: * delegate to the wrapped jdbc 1.x {@link java.sql.Connection}.
296: *
297: * @exception SQLException if this connection is closed or an error occurs
298: * in the wrapped connection.
299: */
300: public PreparedStatement prepareStatement(String sql,
301: int resultSetType, int resultSetConcurrency)
302: throws SQLException {
303: assertOpen();
304: return pooledConnection.prepareStatement(sql, resultSetType,
305: resultSetConcurrency);
306: }
307:
308: /**
309: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
310: *
311: * @exception SQLException if this connection is closed or an error occurs
312: * in the wrapped connection.
313: */
314: public void rollback() throws SQLException {
315: assertOpen();
316: connection.rollback();
317: }
318:
319: /**
320: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
321: *
322: * @exception SQLException if this connection is closed or an error occurs
323: * in the wrapped connection.
324: */
325: public void setAutoCommit(boolean b) throws SQLException {
326: assertOpen();
327: connection.setAutoCommit(b);
328: }
329:
330: /**
331: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
332: *
333: * @exception SQLException if this connection is closed or an error occurs
334: * in the wrapped connection.
335: */
336: public void setCatalog(String catalog) throws SQLException {
337: assertOpen();
338: connection.setCatalog(catalog);
339: }
340:
341: /**
342: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
343: *
344: * @exception SQLException if this connection is closed or an error occurs
345: * in the wrapped connection.
346: */
347: public void setReadOnly(boolean readOnly) throws SQLException {
348: assertOpen();
349: connection.setReadOnly(readOnly);
350: }
351:
352: /**
353: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
354: *
355: * @exception SQLException if this connection is closed or an error occurs
356: * in the wrapped connection.
357: */
358: public void setTransactionIsolation(int level) throws SQLException {
359: assertOpen();
360: connection.setTransactionIsolation(level);
361: }
362:
363: /**
364: * Pass thru method to the wrapped jdbc 1.x {@link java.sql.Connection}.
365: *
366: * @exception SQLException if this connection is closed or an error occurs
367: * in the wrapped connection.
368: */
369: public void setTypeMap(Map map) throws SQLException {
370: assertOpen();
371: connection.setTypeMap(map);
372: }
373:
374: // ------------------- JDBC 3.0 -----------------------------------------
375: // Will be commented by the build process on a JDBC 2.0 system
376:
377: /* JDBC_3_ANT_KEY_BEGIN */
378:
379: public int getHoldability() throws SQLException {
380: assertOpen();
381: return connection.getHoldability();
382: }
383:
384: public void setHoldability(int holdability) throws SQLException {
385: assertOpen();
386: connection.setHoldability(holdability);
387: }
388:
389: public java.sql.Savepoint setSavepoint() throws SQLException {
390: assertOpen();
391: return connection.setSavepoint();
392: }
393:
394: public java.sql.Savepoint setSavepoint(String name)
395: throws SQLException {
396: assertOpen();
397: return connection.setSavepoint(name);
398: }
399:
400: public void rollback(java.sql.Savepoint savepoint)
401: throws SQLException {
402: assertOpen();
403: connection.rollback(savepoint);
404: }
405:
406: public void releaseSavepoint(java.sql.Savepoint savepoint)
407: throws SQLException {
408: assertOpen();
409: connection.releaseSavepoint(savepoint);
410: }
411:
412: public Statement createStatement(int resultSetType,
413: int resultSetConcurrency, int resultSetHoldability)
414: throws SQLException {
415: assertOpen();
416: return connection.createStatement(resultSetType,
417: resultSetConcurrency, resultSetHoldability);
418: }
419:
420: public PreparedStatement prepareStatement(String sql,
421: int resultSetType, int resultSetConcurrency,
422: int resultSetHoldability) throws SQLException {
423: assertOpen();
424: return connection.prepareStatement(sql, resultSetType,
425: resultSetConcurrency, resultSetHoldability);
426: }
427:
428: public CallableStatement prepareCall(String sql, int resultSetType,
429: int resultSetConcurrency, int resultSetHoldability)
430: throws SQLException {
431: assertOpen();
432: return connection.prepareCall(sql, resultSetType,
433: resultSetConcurrency, resultSetHoldability);
434: }
435:
436: public PreparedStatement prepareStatement(String sql,
437: int autoGeneratedKeys) throws SQLException {
438: assertOpen();
439: return connection.prepareStatement(sql, autoGeneratedKeys);
440: }
441:
442: public PreparedStatement prepareStatement(String sql,
443: int columnIndexes[]) throws SQLException {
444: assertOpen();
445: return connection.prepareStatement(sql, columnIndexes);
446: }
447:
448: public PreparedStatement prepareStatement(String sql,
449: String columnNames[]) throws SQLException {
450: assertOpen();
451: return connection.prepareStatement(sql, columnNames);
452: }
453:
454: /* JDBC_3_ANT_KEY_END */
455: }
|