KickJava   Java API By Example, From Geeks To Geeks.

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


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.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.commons.pool.KeyedObjectPool;
26
27 /**
28  * A {@link DelegatingPreparedStatement} that cooperates with
29  * {@link PoolingConnection} to implement a pool of {@link PreparedStatement}s.
30  * <p>
31  * My {@link #close} method returns me to my containing pool. (See {@link PoolingConnection}.)
32  *
33  * @see PoolingConnection
34  * @author Rodney Waldhoff
35  * @author Glenn L. Nielsen
36  * @author James House
37  * @author Dirk Verbeeck
38  * @version $Revision: 1.11 $ $Date: 2004/03/07 10:50:37 $
39  */

40 public class PoolablePreparedStatement extends DelegatingPreparedStatement implements PreparedStatement JavaDoc {
41     /**
42      * The {@link KeyedObjectPool} from which I was obtained.
43      */

44     protected KeyedObjectPool _pool = null;
45
46     /**
47      * My "key" as used by {@link KeyedObjectPool}.
48      */

49     protected Object JavaDoc _key = null;
50
51     /**
52      * Constructor
53      * @param stmt my underlying {@link PreparedStatement}
54      * @param key my key" as used by {@link KeyedObjectPool}
55      * @param pool the {@link KeyedObjectPool} from which I was obtained.
56      * @param conn the {@link Connection} from which I was created
57      */

58     public PoolablePreparedStatement(PreparedStatement JavaDoc stmt, Object JavaDoc key, KeyedObjectPool pool, Connection JavaDoc conn) {
59         super((DelegatingConnection) conn, stmt);
60         _pool = pool;
61         _key = key;
62
63         // Remove from trace now because this statement will be
64
// added by the activate method.
65
if(_conn != null) {
66             _conn.removeTrace(this);
67         }
68     }
69
70     /**
71      * Return me to my pool.
72      */

73     public void close() throws SQLException JavaDoc {
74         if(isClosed()) {
75             throw new SQLException JavaDoc("Already closed");
76         } else {
77             try {
78                 _pool.returnObject(_key,this);
79             } catch(SQLException JavaDoc e) {
80                 throw e;
81             } catch(RuntimeException JavaDoc e) {
82                 throw e;
83             } catch(Exception JavaDoc e) {
84                 throw new SQLNestedException("Cannot close preparedstatement (return to pool failed)", e);
85             }
86         }
87     }
88     
89     protected void activate() throws SQLException JavaDoc{
90         _closed = false;
91         if(_conn != null) {
92             _conn.addTrace(this);
93         }
94         super.activate();
95     }
96   
97     protected void passivate() throws SQLException JavaDoc {
98         _closed = true;
99         if(_conn != null) {
100             _conn.removeTrace(this);
101         }
102
103         // The JDBC spec requires that a statment close any open
104
// ResultSet's when it is closed.
105
// FIXME The PreparedStatement we're wrapping should handle this for us.
106
// See bug 17301 for what could happen when ResultSets are closed twice.
107
List JavaDoc resultSets = getTrace();
108         if( resultSets != null) {
109             ResultSet JavaDoc[] set = (ResultSet JavaDoc[]) resultSets.toArray(new ResultSet JavaDoc[resultSets.size()]);
110             for (int i = 0; i < set.length; i++) {
111                 set[i].close();
112             }
113             clearTrace();
114         }
115         
116         super.passivate();
117     }
118
119 }
120
Popular Tags