| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PoolableConnection |
|
| 5.25;5.25 |
| 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: 1023401 $ $Date: 2010-10-16 21:54:24 -0400 (Sat, 16 Oct 2010) $ | |
| 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 | 2212 | 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 | 0 | super(conn); |
| 46 | 0 | _pool = pool; |
| 47 | 0 | } |
| 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 | */ | |
| 55 | public PoolableConnection(Connection conn, ObjectPool pool, AbandonedConfig config) { | |
| 56 | 2212 | super(conn, config); |
| 57 | 2212 | _pool = pool; |
| 58 | 2212 | } |
| 59 | ||
| 60 | ||
| 61 | /** | |
| 62 | * Returns me to my pool. | |
| 63 | */ | |
| 64 | public synchronized void close() throws SQLException { | |
| 65 | 13186 | if (_closed) { |
| 66 | // already closed | |
| 67 | 10 | return; |
| 68 | } | |
| 69 | ||
| 70 | boolean isUnderlyingConectionClosed; | |
| 71 | try { | |
| 72 | 13176 | isUnderlyingConectionClosed = _conn.isClosed(); |
| 73 | 10 | } catch (SQLException e) { |
| 74 | try { | |
| 75 | 10 | _pool.invalidateObject(this); // XXX should be guarded to happen at most once |
| 76 | 0 | } catch(IllegalStateException ise) { |
| 77 | // pool is closed, so close the connection | |
| 78 | 0 | passivate(); |
| 79 | 0 | getInnermostDelegate().close(); |
| 80 | 10 | } catch (Exception ie) { |
| 81 | // DO NOTHING the original exception will be rethrown | |
| 82 | 0 | } |
| 83 | 10 | throw (SQLException) new SQLException("Cannot close connection (isClosed check failed)").initCause(e); |
| 84 | 13166 | } |
| 85 | ||
| 86 | 13166 | if (!isUnderlyingConectionClosed) { |
| 87 | // Normal close: underlying connection is still open, so we | |
| 88 | // simply need to return this proxy to the pool | |
| 89 | try { | |
| 90 | 13164 | _pool.returnObject(this); // XXX should be guarded to happen at most once |
| 91 | 0 | } catch(IllegalStateException e) { |
| 92 | // pool is closed, so close the connection | |
| 93 | 0 | passivate(); |
| 94 | 0 | getInnermostDelegate().close(); |
| 95 | 0 | } catch(SQLException e) { |
| 96 | 0 | throw e; |
| 97 | 0 | } catch(RuntimeException e) { |
| 98 | 0 | throw e; |
| 99 | 0 | } catch(Exception e) { |
| 100 | 0 | throw (SQLException) new SQLException("Cannot close connection (return to pool failed)").initCause(e); |
| 101 | 13164 | } |
| 102 | } else { | |
| 103 | // Abnormal close: underlying connection closed unexpectedly, so we | |
| 104 | // must destroy this proxy | |
| 105 | try { | |
| 106 | 2 | _pool.invalidateObject(this); // XXX should be guarded to happen at most once |
| 107 | 0 | } catch(IllegalStateException e) { |
| 108 | // pool is closed, so close the connection | |
| 109 | 0 | passivate(); |
| 110 | 0 | getInnermostDelegate().close(); |
| 111 | 0 | } catch (Exception ie) { |
| 112 | // DO NOTHING, "Already closed" exception thrown below | |
| 113 | 2 | } |
| 114 | 2 | throw new SQLException("Already closed."); |
| 115 | } | |
| 116 | 13164 | } |
| 117 | ||
| 118 | /** | |
| 119 | * Actually close my underlying {@link Connection}. | |
| 120 | */ | |
| 121 | public void reallyClose() throws SQLException { | |
| 122 | 2112 | super.close(); |
| 123 | 2102 | } |
| 124 | } | |
| 125 |