1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.dbcp;
19
20 import java.sql.Connection;
21 import java.sql.SQLException;
22 import org.apache.commons.pool.ObjectPool;
23
24 /**
25 * A delegating connection that, rather than closing the underlying
26 * connection, returns itself to an {@link ObjectPool} when
27 * closed.
28 *
29 * @author Rodney Waldhoff
30 * @author Glenn L. Nielsen
31 * @author James House
32 * @version $Revision: 479137 $ $Date: 2006-11-25 08:51:48 -0700 (Sat, 25 Nov 2006) $
33 */
34 public class PoolableConnection extends DelegatingConnection {
35 /** The pool to which I should return. */
36 // TODO: Correct use of the pool requires that this connection is only every returned to the pool once.
37 protected ObjectPool _pool = null;
38
39 /**
40 *
41 * @param conn my underlying connection
42 * @param pool the pool to which I should return when closed
43 */
44 public PoolableConnection(Connection conn, ObjectPool pool) {
45 super(conn);
46 _pool = pool;
47 }
48
49 /**
50 *
51 * @param conn my underlying connection
52 * @param pool the pool to which I should return when closed
53 * @param config the abandoned configuration settings
54 * @deprecated AbandonedConfig is now deprecated.
55 */
56 public PoolableConnection(Connection conn, ObjectPool pool,
57 AbandonedConfig config) {
58 super(conn, config);
59 _pool = pool;
60 }
61
62
63 /**
64 * Returns me to my pool.
65 */
66 public synchronized void close() throws SQLException {
67 boolean isClosed = false;
68 try {
69 isClosed = isClosed();
70 } catch (SQLException e) {
71 try {
72 _pool.invalidateObject(this); // XXX should be guarded to happen at most once
73 } catch (Exception ie) {
74 // DO NOTHING the original exception will be rethrown
75 }
76 throw new SQLNestedException("Cannot close connection (isClosed check failed)", e);
77 }
78 if (isClosed) {
79 try {
80 _pool.invalidateObject(this); // XXX should be guarded to happen at most once
81 } catch (Exception ie) {
82 // DO NOTHING, "Already closed" exception thrown below
83 }
84 throw new SQLException("Already closed.");
85 } else {
86 try {
87 _pool.returnObject(this); // XXX should be guarded to happen at most once
88 } catch(SQLException e) {
89 throw e;
90 } catch(RuntimeException e) {
91 throw e;
92 } catch(Exception e) {
93 throw new SQLNestedException("Cannot close connection (return to pool failed)", e);
94 }
95 }
96 }
97
98 /**
99 * Actually close my underlying {@link Connection}.
100 */
101 public void reallyClose() throws SQLException {
102 super.close();
103 }
104
105 }
106