KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.PrintWriter JavaDoc;
20 import java.sql.CallableStatement JavaDoc;
21 import java.sql.Connection JavaDoc;
22 import java.sql.DatabaseMetaData JavaDoc;
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.SQLWarning JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.NoSuchElementException JavaDoc;
29
30 import javax.sql.DataSource JavaDoc;
31
32 import org.apache.commons.pool.ObjectPool;
33
34 /**
35  * A simple {@link DataSource} implementation that obtains
36  * {@link Connection}s from the specified {@link ObjectPool}.
37  *
38  * @author Rodney Waldhoff
39  * @author Glenn L. Nielsen
40  * @author James House
41  * @author Dirk Verbeeck
42  * @version $Revision: 1.13 $ $Date: 2004/02/28 12:18:17 $
43  */

44 public class PoolingDataSource implements DataSource JavaDoc {
45
46     /** Controls access to the underlying connection */
47     private boolean accessToUnderlyingConnectionAllowed = false;
48
49     public PoolingDataSource() {
50         this(null);
51     }
52
53     public PoolingDataSource(ObjectPool pool) {
54         _pool = pool;
55     }
56
57     public void setPool(ObjectPool pool) throws IllegalStateException JavaDoc, NullPointerException JavaDoc {
58         if(null != _pool) {
59             throw new IllegalStateException JavaDoc("Pool already set");
60         } else if(null == pool) {
61             throw new NullPointerException JavaDoc("Pool must not be null.");
62         } else {
63             _pool = pool;
64         }
65     }
66
67     /**
68      * Returns the value of the accessToUnderlyingConnectionAllowed property.
69      *
70      * @return true if access to the underlying is allowed, false otherwise.
71      */

72     public boolean isAccessToUnderlyingConnectionAllowed() {
73         return this.accessToUnderlyingConnectionAllowed;
74     }
75
76     /**
77      * Sets the value of the accessToUnderlyingConnectionAllowed property.
78      * It controls if the PoolGuard allows access to the underlying connection.
79      * (Default: false)
80      *
81      * @param allow Access to the underlying connection is granted when true.
82      */

83     public void setAccessToUnderlyingConnectionAllowed(boolean allow) {
84         this.accessToUnderlyingConnectionAllowed = allow;
85     }
86     
87     //--- DataSource methods -----------------------------------------
88

89     /**
90      * Return a {@link java.sql.Connection} from my pool,
91      * according to the contract specified by {@link ObjectPool#borrowObject}.
92      */

93     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
94         try {
95             Connection JavaDoc conn = (Connection JavaDoc)(_pool.borrowObject());
96             if (conn != null) {
97                 conn = new PoolGuardConnectionWrapper(conn);
98             }
99             return conn;
100         } catch(SQLException JavaDoc e) {
101             throw e;
102         } catch(NoSuchElementException JavaDoc e) {
103             throw new SQLNestedException("Cannot get a connection, pool exhausted", e);
104         } catch(RuntimeException JavaDoc e) {
105             throw e;
106         } catch(Exception JavaDoc e) {
107             throw new SQLNestedException("Cannot get a connection, general error", e);
108         }
109     }
110
111     /**
112      * Throws {@link UnsupportedOperationException}
113      * @throws UnsupportedOperationException
114      */

115     public Connection JavaDoc getConnection(String JavaDoc uname, String JavaDoc passwd) throws SQLException JavaDoc {
116         throw new UnsupportedOperationException JavaDoc();
117     }
118
119     /**
120      * Returns my log writer.
121      * @return my log writer
122      * @see DataSource#getLogWriter
123      */

124     public PrintWriter JavaDoc getLogWriter() {
125         return _logWriter;
126     }
127
128     /**
129      * Throws {@link UnsupportedOperationException}.
130      * Do this configuration within my {@link ObjectPool}.
131      * @throws UnsupportedOperationException
132      */

133     public int getLoginTimeout() {
134         throw new UnsupportedOperationException JavaDoc();
135     }
136
137     /**
138      * Throws {@link UnsupportedOperationException}.
139      * Do this configuration within my {@link ObjectPool}.
140      * @throws UnsupportedOperationException
141      */

142     public void setLoginTimeout(int seconds) {
143         throw new UnsupportedOperationException JavaDoc();
144     }
145
146     /**
147      * Sets my log writer.
148      * @see DataSource#setLogWriter
149      */

150     public void setLogWriter(PrintWriter JavaDoc out) {
151         _logWriter = out;
152     }
153
154     /** My log writer. */
155     protected PrintWriter JavaDoc _logWriter = null;
156
157     protected ObjectPool _pool = null;
158
159     /**
160      * PoolGuardConnectionWrapper is a Connection wrapper that makes sure a
161      * closed connection cannot be used anymore.
162      */

163     private class PoolGuardConnectionWrapper extends DelegatingConnection {
164
165         private Connection JavaDoc delegate;
166     
167         PoolGuardConnectionWrapper(Connection JavaDoc delegate) {
168             super(delegate);
169             this.delegate = delegate;
170         }
171
172         protected void checkOpen() throws SQLException JavaDoc {
173             if(delegate == null) {
174                 throw new SQLException JavaDoc("Connection is closed.");
175             }
176         }
177     
178         public void close() throws SQLException JavaDoc {
179             checkOpen();
180             this.delegate.close();
181             this.delegate = null;
182             super.setDelegate(null);
183         }
184
185         public boolean isClosed() throws SQLException JavaDoc {
186             if (delegate == null) {
187                 return true;
188             }
189             return delegate.isClosed();
190         }
191
192         public void clearWarnings() throws SQLException JavaDoc {
193             checkOpen();
194             delegate.clearWarnings();
195         }
196
197         public void commit() throws SQLException JavaDoc {
198             checkOpen();
199             delegate.commit();
200         }
201
202         public Statement JavaDoc createStatement() throws SQLException JavaDoc {
203             checkOpen();
204             return delegate.createStatement();
205         }
206
207         public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
208             checkOpen();
209             return delegate.createStatement(resultSetType, resultSetConcurrency);
210         }
211
212         public boolean equals(Object JavaDoc obj) {
213             if (delegate == null){
214                 return false;
215             }
216             return delegate.equals(obj);
217         }
218
219         public boolean getAutoCommit() throws SQLException JavaDoc {
220             checkOpen();
221             return delegate.getAutoCommit();
222         }
223
224         public String JavaDoc getCatalog() throws SQLException JavaDoc {
225             checkOpen();
226             return delegate.getCatalog();
227         }
228
229         public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
230             checkOpen();
231             return delegate.getMetaData();
232         }
233
234         public int getTransactionIsolation() throws SQLException JavaDoc {
235             checkOpen();
236             return delegate.getTransactionIsolation();
237         }
238
239         public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
240             checkOpen();
241             return delegate.getTypeMap();
242         }
243
244         public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
245             checkOpen();
246             return delegate.getWarnings();
247         }
248
249         public int hashCode() {
250             if (delegate == null){
251                 return 0;
252             }
253             return delegate.hashCode();
254         }
255
256         public boolean isReadOnly() throws SQLException JavaDoc {
257             checkOpen();
258             return delegate.isReadOnly();
259         }
260
261         public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
262             checkOpen();
263             return delegate.nativeSQL(sql);
264         }
265
266         public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
267             checkOpen();
268             return delegate.prepareCall(sql);
269         }
270
271         public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
272             checkOpen();
273             return delegate.prepareCall(sql, resultSetType, resultSetConcurrency);
274         }
275
276         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
277             checkOpen();
278             return delegate.prepareStatement(sql);
279         }
280
281         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
282             checkOpen();
283             return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency);
284         }
285
286         public void rollback() throws SQLException JavaDoc {
287             checkOpen();
288             delegate.rollback();
289         }
290
291         public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc {
292             checkOpen();
293             delegate.setAutoCommit(autoCommit);
294         }
295
296         public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
297             checkOpen();
298             delegate.setCatalog(catalog);
299         }
300
301         public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
302             checkOpen();
303             delegate.setReadOnly(readOnly);
304         }
305
306         public void setTransactionIsolation(int level) throws SQLException JavaDoc {
307             checkOpen();
308             delegate.setTransactionIsolation(level);
309         }
310
311         public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
312             checkOpen();
313             delegate.setTypeMap(map);
314         }
315
316         public String JavaDoc toString() {
317             if (delegate == null){
318                 return null;
319             }
320             return delegate.toString();
321         }
322
323         // ------------------- JDBC 3.0 -----------------------------------------
324
// Will be commented by the build process on a JDBC 2.0 system
325

326 /* JDBC_3_ANT_KEY_BEGIN */
327
328         public int getHoldability() throws SQLException JavaDoc {
329             checkOpen();
330             return delegate.getHoldability();
331         }
332     
333         public void setHoldability(int holdability) throws SQLException JavaDoc {
334             checkOpen();
335             delegate.setHoldability(holdability);
336         }
337
338         public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
339             checkOpen();
340             return delegate.setSavepoint();
341         }
342
343         public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
344             checkOpen();
345             return delegate.setSavepoint(name);
346         }
347
348         public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
349             checkOpen();
350             delegate.releaseSavepoint(savepoint);
351         }
352
353         public void rollback(java.sql.Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
354             checkOpen();
355             delegate.rollback(savepoint);
356         }
357
358         public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
359             checkOpen();
360             return delegate.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
361         }
362
363         public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
364             checkOpen();
365             return delegate.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
366         }
367
368         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
369             checkOpen();
370             return delegate.prepareStatement(sql, autoGeneratedKeys);
371         }
372
373         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException JavaDoc {
374             checkOpen();
375             return delegate.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
376         }
377
378         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
379             checkOpen();
380             return delegate.prepareStatement(sql, columnIndexes);
381         }
382
383         public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
384             checkOpen();
385             return delegate.prepareStatement(sql, columnNames);
386         }
387
388 /* JDBC_3_ANT_KEY_END */
389
390         /**
391          * @see org.apache.commons.dbcp.DelegatingConnection#getDelegate()
392          */

393         public Connection JavaDoc getDelegate() {
394             if (isAccessToUnderlyingConnectionAllowed()) {
395                 return super.getDelegate();
396             } else {
397                 return null;
398             }
399         }
400
401         /**
402          * @see org.apache.commons.dbcp.DelegatingConnection#getInnermostDelegate()
403          */

404         public Connection JavaDoc getInnermostDelegate() {
405             if (isAccessToUnderlyingConnectionAllowed()) {
406                 return super.getInnermostDelegate();
407             } else {
408                 return null;
409             }
410         }
411     }
412 }
413
Popular Tags