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;
019:
020: import java.sql.Connection;
021: import java.sql.SQLException;
022: import org.apache.commons.pool.ObjectPool;
023:
024: /**
025: * A delegating connection that, rather than closing the underlying
026: * connection, returns itself to an {@link ObjectPool} when
027: * closed.
028: *
029: * @author Rodney Waldhoff
030: * @author Glenn L. Nielsen
031: * @author James House
032: * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
033: */
034: public class PoolableConnection extends DelegatingConnection {
035: /** The pool to which I should return. */
036: // TODO: Correct use of the pool requires that this connection is only every returned to the pool once.
037: protected ObjectPool _pool = null;
038:
039: /**
040: *
041: * @param conn my underlying connection
042: * @param pool the pool to which I should return when closed
043: */
044: public PoolableConnection(Connection conn, ObjectPool pool) {
045: super (conn);
046: _pool = pool;
047: }
048:
049: /**
050: *
051: * @param conn my underlying connection
052: * @param pool the pool to which I should return when closed
053: * @param config the abandoned configuration settings
054: * @deprecated AbandonedConfig is now deprecated.
055: */
056: public PoolableConnection(Connection conn, ObjectPool pool,
057: AbandonedConfig config) {
058: super (conn, config);
059: _pool = pool;
060: }
061:
062: /**
063: * Returns me to my pool.
064: */
065: public synchronized void close() throws SQLException {
066: boolean isClosed = false;
067: try {
068: isClosed = isClosed();
069: } catch (SQLException e) {
070: try {
071: _pool.invalidateObject(this ); // XXX should be guarded to happen at most once
072: } catch (Exception ie) {
073: // DO NOTHING the original exception will be rethrown
074: }
075: throw new SQLNestedException(
076: "Cannot close connection (isClosed check failed)",
077: e);
078: }
079: if (isClosed) {
080: try {
081: _pool.invalidateObject(this ); // XXX should be guarded to happen at most once
082: } catch (Exception ie) {
083: // DO NOTHING, "Already closed" exception thrown below
084: }
085: throw new SQLException("Already closed.");
086: } else {
087: try {
088: _pool.returnObject(this ); // XXX should be guarded to happen at most once
089: } catch (SQLException e) {
090: throw e;
091: } catch (RuntimeException e) {
092: throw e;
093: } catch (Exception e) {
094: throw new SQLNestedException(
095: "Cannot close connection (return to pool failed)",
096: e);
097: }
098: }
099: }
100:
101: /**
102: * Actually close my underlying {@link Connection}.
103: */
104: public void reallyClose() throws SQLException {
105: super.close();
106: }
107:
108: }
|