KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
20 import java.util.NoSuchElementException JavaDoc;
21
22 import org.apache.commons.pool.*;
23
24 /**
25  * A {@link DelegatingConnection} that pools {@link PreparedStatement}s.
26  * <p>
27  * My {@link #prepareStatement} methods, rather than creating a new {@link PreparedStatement}
28  * each time, may actually pull the {@link PreparedStatement} from a pool of unused statements.
29  * The {@link PreparedStatement#close} method of the returned {@link PreparedStatement} doesn't
30  * actually close the statement, but rather returns it to my pool. (See {@link PoolablePreparedStatement}.)
31  *
32  * @see PoolablePreparedStatement
33  * @author Rodney Waldhoff
34  * @author Dirk Verbeeck
35  * @version $Revision: 1.14 $ $Date: 2004/03/07 15:26:38 $
36  */

37 public class PoolingConnection extends DelegatingConnection implements Connection, KeyedPoolableObjectFactory {
38     /** My pool of {@link PreparedStatement}s. */
39     protected KeyedObjectPool _pstmtPool = null;
40
41     /**
42      * Constructor.
43      * @param c the underlying {@link Connection}.
44      */

45     public PoolingConnection(Connection c) {
46         super(c);
47     }
48
49     /**
50      * Constructor.
51      * @param c the underlying {@link Connection}.
52      * @param maxSleepingPerKey the maximum number of {@link PreparedStatement}s that may sit idle in my pool (per type)
53      */

54     public PoolingConnection(Connection c, KeyedObjectPool pool) {
55         super(c);
56         _pstmtPool = pool;
57     }
58
59
60     /**
61      * Close and free all {@link PreparedStatement}s from my pool, and
62      * close my underlying connection.
63      */

64     public synchronized void close() throws SQLException {
65         if(null != _pstmtPool) {
66             KeyedObjectPool oldpool = _pstmtPool;
67             _pstmtPool = null;
68             try {
69                 oldpool.close();
70             } catch(RuntimeException JavaDoc e) {
71                 throw e;
72             } catch(SQLException e) {
73                 throw e;
74             } catch(Exception JavaDoc e) {
75                 throw new SQLNestedException("Cannot close connection", e);
76             }
77         }
78         getInnermostDelegate().close();
79     }
80
81     /**
82      * Create or obtain a {@link PreparedStatement} from my pool.
83      * @return a {@link PoolablePreparedStatement}
84      */

85     public synchronized PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
86         try {
87             return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql)));
88         } catch(NoSuchElementException JavaDoc e) {
89             throw new SQLNestedException("MaxOpenPreparedStatements limit reached", e);
90         } catch(RuntimeException JavaDoc e) {
91             throw e;
92         } catch(Exception JavaDoc e) {
93             throw new SQLNestedException("Borrow prepareStatement from pool failed", e);
94         }
95     }
96
97     /**
98      * Create or obtain a {@link PreparedStatement} from my pool.
99      * @return a {@link PoolablePreparedStatement}
100      */

101     public synchronized PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException {
102         try {
103             return(PreparedStatement)(_pstmtPool.borrowObject(createKey(sql,resultSetType,resultSetConcurrency)));
104         } catch(NoSuchElementException JavaDoc e) {
105             throw new SQLNestedException("MaxOpenPreparedStatements limit reached", e);
106         } catch(RuntimeException JavaDoc e) {
107             throw e;
108         } catch(Exception JavaDoc e) {
109             throw new SQLNestedException("Borrow prepareStatement from pool failed", e);
110         }
111     }
112
113     // ------------------- JDBC 3.0 -----------------------------------------
114
// Will be commented by the build process on a JDBC 2.0 system
115

116 /* JDBC_3_ANT_KEY_BEGIN */
117
118 // TODO: possible enhancement, cache these preparedStatements as well
119

120 // public PreparedStatement prepareStatement(String sql, int resultSetType,
121
// int resultSetConcurrency,
122
// int resultSetHoldability)
123
// throws SQLException {
124
// return super.prepareStatement(
125
// sql, resultSetType, resultSetConcurrency, resultSetHoldability);
126
// }
127
//
128
// public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
129
// throws SQLException {
130
// return super.prepareStatement(sql, autoGeneratedKeys);
131
// }
132
//
133
// public PreparedStatement prepareStatement(String sql, int columnIndexes[])
134
// throws SQLException {
135
// return super.prepareStatement(sql, columnIndexes);
136
// }
137
//
138
// public PreparedStatement prepareStatement(String sql, String columnNames[])
139
// throws SQLException {
140
// return super.prepareStatement(sql, columnNames);
141
// }
142

143 /* JDBC_3_ANT_KEY_END */
144
145
146     /**
147      * Create a PStmtKey for the given arguments.
148      */

149     protected Object JavaDoc createKey(String JavaDoc sql, int resultSetType, int resultSetConcurrency) {
150         String JavaDoc catalog = null;
151         try {
152             catalog = getCatalog();
153         } catch (Exception JavaDoc e) {}
154         return new PStmtKey(normalizeSQL(sql), catalog, resultSetType, resultSetConcurrency);
155     }
156
157     /**
158      * Create a PStmtKey for the given arguments.
159      */

160     protected Object JavaDoc createKey(String JavaDoc sql) {
161         String JavaDoc catalog = null;
162         try {
163             catalog = getCatalog();
164         } catch (Exception JavaDoc e) {}
165         return new PStmtKey(normalizeSQL(sql), catalog);
166     }
167
168     /**
169      * Normalize the given SQL statement, producing a
170      * cannonical form that is semantically equivalent to the original.
171      */

172     protected String JavaDoc normalizeSQL(String JavaDoc sql) {
173         return sql.trim();
174     }
175
176     /**
177      * My {@link KeyedPoolableObjectFactory} method for creating
178      * {@link PreparedStatement}s.
179      * @param obj the key for the {@link PreparedStatement} to be created
180      */

181     public Object JavaDoc makeObject(Object JavaDoc obj) throws Exception JavaDoc {
182         if(null == obj || !(obj instanceof PStmtKey)) {
183             throw new IllegalArgumentException JavaDoc();
184         } else {
185             // _openPstmts++;
186
PStmtKey key = (PStmtKey)obj;
187             if(null == key._resultSetType && null == key._resultSetConcurrency) {
188                 return new PoolablePreparedStatement(getDelegate().prepareStatement(key._sql),key,_pstmtPool,this);
189             } else {
190                 return new PoolablePreparedStatement(getDelegate().prepareStatement(key._sql,key._resultSetType.intValue(),key._resultSetConcurrency.intValue()),key,_pstmtPool,this);
191             }
192         }
193     }
194
195     /**
196      * My {@link KeyedPoolableObjectFactory} method for destroying
197      * {@link PreparedStatement}s.
198      * @param key ignored
199      * @param obj the {@link PreparedStatement} to be destroyed.
200      */

201     public void destroyObject(Object JavaDoc key, Object JavaDoc obj) throws Exception JavaDoc {
202         //_openPstmts--;
203
if(obj instanceof DelegatingPreparedStatement) {
204             ((DelegatingPreparedStatement)obj).getInnermostDelegate().close();
205         } else {
206             ((PreparedStatement)obj).close();
207         }
208     }
209
210     /**
211      * My {@link KeyedPoolableObjectFactory} method for validating
212      * {@link PreparedStatement}s.
213      * @param key ignored
214      * @param obj ignored
215      * @return <tt>true</tt>
216      */

217     public boolean validateObject(Object JavaDoc key, Object JavaDoc obj) {
218         return true;
219     }
220
221     /**
222      * My {@link KeyedPoolableObjectFactory} method for activating
223      * {@link PreparedStatement}s. (Currently a no-op.)
224      * @param key ignored
225      * @param obj ignored
226      */

227     public void activateObject(Object JavaDoc key, Object JavaDoc obj) throws Exception JavaDoc {
228         ((DelegatingPreparedStatement)obj).activate();
229     }
230
231     /**
232      * My {@link KeyedPoolableObjectFactory} method for passivating
233      * {@link PreparedStatement}s. Currently invokes {@link PreparedStatement#clearParameters}.
234      * @param key ignored
235      * @param obj a {@link PreparedStatement}
236      */

237     public void passivateObject(Object JavaDoc key, Object JavaDoc obj) throws Exception JavaDoc {
238         ((PreparedStatement)obj).clearParameters();
239         ((DelegatingPreparedStatement)obj).passivate();
240     }
241
242     public String JavaDoc toString() {
243         return "PoolingConnection: " + _pstmtPool.toString();
244     }
245
246     /**
247      * A key uniquely identifiying {@link PreparedStatement}s.
248      */

249     class PStmtKey {
250         protected String JavaDoc _sql = null;
251         protected Integer JavaDoc _resultSetType = null;
252         protected Integer JavaDoc _resultSetConcurrency = null;
253         protected String JavaDoc _catalog = null;
254         
255         PStmtKey(String JavaDoc sql) {
256             _sql = sql;
257         }
258
259         PStmtKey(String JavaDoc sql, String JavaDoc catalog) {
260             _sql = sql;
261             _catalog = catalog;
262         }
263
264         PStmtKey(String JavaDoc sql, int resultSetType, int resultSetConcurrency) {
265             _sql = sql;
266             _resultSetType = new Integer JavaDoc(resultSetType);
267             _resultSetConcurrency = new Integer JavaDoc(resultSetConcurrency);
268         }
269
270         PStmtKey(String JavaDoc sql, String JavaDoc catalog, int resultSetType, int resultSetConcurrency) {
271             _sql = sql;
272             _catalog = catalog;
273             _resultSetType = new Integer JavaDoc(resultSetType);
274             _resultSetConcurrency = new Integer JavaDoc(resultSetConcurrency);
275         }
276
277         public boolean equals(Object JavaDoc that) {
278             try {
279                 PStmtKey key = (PStmtKey)that;
280                 return( ((null == _sql && null == key._sql) || _sql.equals(key._sql)) &&
281                         ((null == _catalog && null == key._catalog) || _catalog.equals(key._catalog)) &&
282                         ((null == _resultSetType && null == key._resultSetType) || _resultSetType.equals(key._resultSetType)) &&
283                         ((null == _resultSetConcurrency && null == key._resultSetConcurrency) || _resultSetConcurrency.equals(key._resultSetConcurrency))
284                       );
285             } catch(ClassCastException JavaDoc e) {
286                 return false;
287             } catch(NullPointerException JavaDoc e) {
288                 return false;
289             }
290         }
291
292         public int hashCode() {
293             if (_catalog==null)
294                 return(null == _sql ? 0 : _sql.hashCode());
295             else
296                 return(null == _sql ? _catalog.hashCode() : (_catalog + _sql).hashCode());
297         }
298
299         public String JavaDoc toString() {
300             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
301             buf.append("PStmtKey: sql=");
302             buf.append(_sql);
303             buf.append(", catalog=");
304             buf.append(_catalog);
305             buf.append(", resultSetType=");
306             buf.append(_resultSetType);
307             buf.append(", resultSetConcurrency=");
308             buf.append(_resultSetConcurrency);
309             return buf.toString();
310         }
311     }
312 }
313
Popular Tags