KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > jdbc > datasource > JDBCDataSource


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.jdbc.datasource;
24
25 import java.io.PrintWriter JavaDoc;
26 import java.sql.*;
27 import java.util.Properties JavaDoc;
28
29 import javax.sql.DataSource JavaDoc;
30
31 import org.apache.commons.dbcp.*;
32 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
33 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
34 import org.apache.commons.pool.impl.GenericObjectPool;
35
36
37 public class JDBCDataSource implements DataSource JavaDoc {
38     
39     /**
40      * Default max cached prepared statements
41      */

42     private static final int DEFAULT_MAX_OPEN_STATEMENTS = 100;
43
44     /**
45      * The JDBC driver used by the DriverConnectionFactory to establish a connection
46      */

47     protected Driver driver = null;
48     
49     /**
50      * The connection URL to be passed to our JDBC driver to establish
51      * a connection.
52      */

53     protected String JavaDoc url = null;
54
55     /**
56      * The connection properties that will be sent to the JDBC driver when
57      * establishing new connections.
58      */

59     protected Properties JavaDoc connectionProperties = null;
60
61     /**
62      * The object pool that internally manages connections.
63      */

64     protected GenericObjectPool connectionPool = null;
65
66     /**
67      * The connection factory used by the pool to create physical connections
68      */

69     protected PoolableConnectionFactory connectionFactory = null;
70     
71     /**
72      * The data source we will use to manage connections.
73      */

74     protected PoolingDataSource dataSource = null;
75     
76     JDBCDataSource(Driver driver, String JavaDoc url, Properties JavaDoc connectionProperties) throws SQLException {
77         this(driver, url, connectionProperties, DEFAULT_MAX_OPEN_STATEMENTS);
78     }
79     
80     JDBCDataSource(Driver driver, String JavaDoc url, Properties JavaDoc connectionProperties, int maxOpenPreparedStatements)
81     throws SQLException
82     {
83         this.driver = driver;
84         this.url = url;
85         this.connectionProperties = connectionProperties;
86
87         connectionPool = new GenericObjectPool();
88         connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
89         GenericKeyedObjectPoolFactory statementPoolFactory = null;
90         if (maxOpenPreparedStatements >= 0) {
91             statementPoolFactory = new GenericKeyedObjectPoolFactory(
92                         null,
93                         -1, // unlimited maxActive (per key)
94
GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL,
95                         0, // maxWait
96
1, // maxIdle (per key)
97
maxOpenPreparedStatements);
98         }
99
100         DriverConnectionFactory driverConnectionFactory =
101             new DriverConnectionFactory(driver, url, connectionProperties);
102
103         connectionFactory =
104             new PoolableConnectionFactory(driverConnectionFactory,
105                                           connectionPool,
106                                           statementPoolFactory,
107                                           null,
108                                           false,
109                                           true);
110         if (connectionFactory == null) {
111             throw new SQLException("Cannot create PoolableConnectionFactory");
112         }
113         dataSource = new PoolingDataSource(connectionPool);
114     }
115
116     /**
117      * Close and release all connections that are currently stored in the
118      * connection pool associated with our data source.
119      *
120      * @exception SQLException if a database error occurs
121      */

122     public void close() throws SQLException {
123         GenericObjectPool oldpool = connectionPool;
124         connectionPool = null;
125         dataSource = null;
126         try {
127             if (oldpool != null) {
128                 oldpool.close();
129             }
130         } catch(SQLException e) {
131             throw e;
132         } catch(RuntimeException JavaDoc e) {
133             throw e;
134         } catch(Exception JavaDoc e) {
135             throw new SQLNestedException("Cannot close connection pool", e);
136         }
137     }
138
139     /**
140      * Create (if necessary) and return a connection to the database.
141      *
142      * @exception SQLException if a database access error occurs
143      */

144     public Connection getConnection() throws SQLException {
145         return dataSource.getConnection();
146     }
147
148     /**
149      * Create (if necessary) and return a connection to the database.
150      *
151      * @param username Database user on whose behalf the Connection
152      * is being made
153      * @param password The database user's password
154      *
155      * @exception SQLException if a database access error occurs
156      */

157     public Connection getConnection(String JavaDoc username, String JavaDoc password) throws SQLException {
158         return dataSource.getConnection(username, password);
159     }
160
161     /**
162      * Return the login timeout (in seconds) for connecting to the database.
163      *
164      * @exception SQLException if a database access error occurs
165      */

166     public int getLoginTimeout() throws SQLException {
167         return dataSource.getLoginTimeout();
168     }
169
170     /**
171      * Return the log writer being used by this data source.
172      *
173      * @exception SQLException if a database access error occurs
174      */

175     public PrintWriter JavaDoc getLogWriter() throws SQLException {
176         return dataSource.getLogWriter();
177     }
178
179     /**
180      * Set the login timeout (in seconds) for connecting to the database.
181      *
182      * @param loginTimeout The new login timeout, or zero for no timeout
183      *
184      * @exception SQLException if a database access error occurs
185      */

186     public void setLoginTimeout(int loginTimeout) throws SQLException {
187         dataSource.setLoginTimeout(loginTimeout);
188     }
189
190     /**
191      * Set the log writer being used by this data source.
192      *
193      * @param logWriter The new log writer
194      *
195      * @exception SQLException if a database access error occurs
196      */

197     public void setLogWriter(PrintWriter JavaDoc logWriter) throws SQLException {
198         dataSource.setLogWriter(logWriter);
199     }
200
201     public Driver getDriver() {
202         return this.driver;
203     }
204     
205     public String JavaDoc getUrl() {
206         return this.url;
207     }
208
209     public String JavaDoc getUsername() {
210         return this.connectionProperties.getProperty("user");
211     }
212
213     public String JavaDoc getPassword() {
214         return this.connectionProperties.getProperty("password");
215     }
216
217     public void setDefaultAutoCommit(boolean defaultAutoCommit) {
218         connectionFactory.setDefaultAutoCommit(defaultAutoCommit);
219     }
220
221     public void setDefaultReadOnly(boolean defaultReadOnly) {
222         connectionFactory.setDefaultReadOnly(defaultReadOnly);
223     }
224
225     public void setDefaultTransactionIsolation(int defaultTransactionIsolation) {
226         connectionFactory.setDefaultTransactionIsolation(defaultTransactionIsolation);
227     }
228
229     public void setDefaultCatalog(String JavaDoc defaultCatalog) {
230         connectionFactory.setDefaultCatalog(defaultCatalog);
231     }
232
233     public void setValidationQuery(String JavaDoc validationQuery) {
234         if ((validationQuery != null) && (validationQuery.trim().length() > 0)) {
235             connectionFactory.setValidationQuery(validationQuery);
236         } else {
237             connectionFactory.setValidationQuery(null);
238         }
239     }
240
241     public boolean isAccessToUnderlyingConnectionAllowed() {
242         return dataSource.isAccessToUnderlyingConnectionAllowed();
243     }
244
245     public void setAccessToUnderlyingConnectionAllowed(boolean allow) {
246         dataSource.setAccessToUnderlyingConnectionAllowed(allow);
247     }
248
249     // Connection pool management
250

251     /**
252      * [Read Only] The current number of active connections that have been
253      * allocated from this data source.
254      */

255     public int getNumActive() {
256         if (connectionPool != null) {
257             return connectionPool.getNumActive();
258         } else {
259             return 0;
260         }
261     }
262
263     /**
264      * [Read Only] The current number of idle connections that are waiting
265      * to be allocated from this data source.
266      */

267     public int getNumIdle() {
268         if (connectionPool != null) {
269             return connectionPool.getNumIdle();
270         } else {
271             return 0;
272         }
273     }
274
275     public int getMaxActive() {
276         return connectionPool.getMaxActive();
277     }
278
279     public void setMaxActive(int maxActive) {
280         connectionPool.setMaxActive(maxActive);
281     }
282
283     public int getMaxIdle() {
284         return connectionPool.getMaxIdle();
285     }
286
287     public void setMaxIdle(int maxIdle) {
288         connectionPool.setMaxIdle(maxIdle);
289     }
290
291     public int getMinIdle() {
292         return connectionPool.getMinIdle();
293     }
294
295     public void setMinIdle(int minIdle) {
296         connectionPool.setMinIdle(minIdle);
297     }
298
299     public long getMaxWait() {
300         return connectionPool.getMaxWait();
301     }
302
303     public void setMaxWait(long maxWait) {
304         connectionPool.setMaxWait(maxWait);
305     }
306     
307     public byte getWhenExhaustedAction() {
308         return connectionPool.getWhenExhaustedAction();
309     }
310     
311     public void setWhenExhaustedAction(byte whenExhaustedAction) {
312         connectionPool.setWhenExhaustedAction(whenExhaustedAction);
313     }
314
315     public boolean getTestOnBorrow() {
316         return connectionPool.getTestOnBorrow();
317     }
318
319     public void setTestOnBorrow(boolean testOnBorrow) {
320         connectionPool.setTestOnBorrow(testOnBorrow);
321     }
322
323     public boolean getTestOnReturn() {
324         return connectionPool.getTestOnReturn();
325     }
326
327     public void setTestOnReturn(boolean testOnReturn) {
328         connectionPool.setTestOnReturn(testOnReturn);
329     }
330     
331     public long getTimeBetweenEvictionRunsMillis() {
332         return connectionPool.getTimeBetweenEvictionRunsMillis();
333     }
334
335     public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
336         connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
337     }
338
339     public int getNumTestsPerEvictionRun() {
340         return connectionPool.getNumTestsPerEvictionRun();
341     }
342
343     public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
344         connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
345     }
346
347     public long getMinEvictableIdleTimeMillis() {
348         return connectionPool.getMinEvictableIdleTimeMillis();
349     }
350
351     public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
352         connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
353     }
354
355     public boolean getTestWhileIdle() {
356         return connectionPool.getTestWhileIdle();
357     }
358
359     public void setTestWhileIdle(boolean testWhileIdle) {
360         connectionPool.setTestWhileIdle(testWhileIdle);
361     }
362
363 }
364
Popular Tags