KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbcp > PoolableConnection


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;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import org.apache.commons.pool.ObjectPool;
22
23 /**
24  * A delegating connection that, rather than closing the underlying
25  * connection, returns itself to an {@link ObjectPool} when
26  * closed.
27  *
28  * @author Rodney Waldhoff
29  * @author Glenn L. Nielsen
30  * @author James House
31  * @version $Revision: 1.14 $ $Date: 2004/05/01 12:50:12 $
32  */

33 public class PoolableConnection extends DelegatingConnection {
34     /** The pool to which I should return. */
35     protected ObjectPool _pool = null;
36
37     /**
38      *
39      * @param conn my underlying connection
40      * @param pool the pool to which I should return when closed
41      */

42     public PoolableConnection(Connection JavaDoc conn, ObjectPool pool) {
43         super(conn);
44         _pool = pool;
45     }
46
47     /**
48      *
49      * @param conn my underlying connection
50      * @param pool the pool to which I should return when closed
51      * @param config the abandoned configuration settings
52      * @deprecated AbandonedConfig is now deprecated.
53      */

54     public PoolableConnection(Connection JavaDoc conn, ObjectPool pool,
55                               AbandonedConfig config) {
56         super(conn, config);
57         _pool = pool;
58     }
59
60
61     /**
62      * Returns me to my pool.
63      */

64      public synchronized void close() throws SQLException JavaDoc {
65         boolean isClosed = false;
66         try {
67             isClosed = isClosed();
68         } catch (SQLException JavaDoc e) {
69             try {
70                 _pool.invalidateObject(this);
71             } catch (Exception JavaDoc ie) {
72                 // DO NOTHING the original exception will be rethrown
73
}
74             throw new SQLNestedException("Cannot close connection (isClosed check failed)", e);
75         }
76         if (isClosed) {
77             throw new SQLException JavaDoc("Already closed.");
78         } else {
79             try {
80                 _pool.returnObject(this);
81             } catch(SQLException JavaDoc e) {
82                 throw e;
83             } catch(RuntimeException JavaDoc e) {
84                 throw e;
85             } catch(Exception JavaDoc e) {
86                 throw new SQLNestedException("Cannot close connection (return to pool failed)", e);
87             }
88         }
89     }
90
91     /**
92      * Actually close my underlying {@link Connection}.
93      */

94     public void reallyClose() throws SQLException JavaDoc {
95         super.close();
96     }
97
98 }
99
100
Popular Tags