KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > utils > PoolingConnectionProvider


1 /*
2  * Copyright 2004-2005 OpenSymphony
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  *
16  */

17
18 /*
19  * Previously Copyright (c) 2001-2004 James House
20  */

21 package org.quartz.utils;
22
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import org.apache.commons.dbcp.BasicDataSource;
28
29 /**
30  * <p>
31  * A <code>ConnectionProvider</code> implementation that creates its own
32  * pool of connections.
33  * </p>
34  *
35  * <p>
36  * This class uses <a HREF="http://jakarta.apache.org/commons/dbcp/">DBCP</a>,
37  * an Apache-Jakarta-Commons product.
38  * </p>
39  *
40  * @see DBConnectionManager
41  * @see ConnectionProvider
42  *
43  * @author Sharada Jambula
44  * @author James House
45  * @author Mohammad Rezaei
46  */

47 public class PoolingConnectionProvider implements ConnectionProvider {
48
49     /*
50      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
51      *
52      * Constants.
53      *
54      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55      */

56
57     /** The JDBC database driver. */
58     public static final String JavaDoc DB_DRIVER = "driver";
59
60     /** The JDBC database URL. */
61     public static final String JavaDoc DB_URL = "URL";
62
63     /** The database user name. */
64     public static final String JavaDoc DB_USER = "user";
65
66     /** The database user password. */
67     public static final String JavaDoc DB_PASSWORD = "password";
68
69     /** The maximum number of database connections to have in the pool. */
70     public static final String JavaDoc DB_MAX_CONNECTIONS = "maxConnections";
71
72     /**
73      * The database sql query to execute everytime a connection is retrieved
74      * from the pool to ensure that it is still valid.
75      */

76     public static final String JavaDoc DB_VALIDATION_QUERY = "validationQuery";
77     
78     /** Default maximum number of database connections in the pool. */
79     public static final int DEFAULT_DB_MAX_CONNECTIONS = 10;
80
81     /*
82      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
83      *
84      * Data members.
85      *
86      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87      */

88
89     private BasicDataSource datasource;
90
91     /*
92      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93      *
94      * Constructors.
95      *
96      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97      */

98
99     public PoolingConnectionProvider(String JavaDoc dbDriver, String JavaDoc dbURL,
100             String JavaDoc dbUser, String JavaDoc dbPassword, int maxConnections,
101             String JavaDoc dbValidationQuery) throws SQLException JavaDoc {
102         initialize(
103             dbDriver, dbURL, dbUser, dbPassword,
104             maxConnections, dbValidationQuery);
105     }
106
107     /**
108      * Create a connection pool using the given properties.
109      *
110      * <p>
111      * The properties passed should contain:
112      * <UL>
113      * <LI>{@link #DB_DRIVER}- The database driver class name
114      * <LI>{@link #DB_URL}- The database URL
115      * <LI>{@link #DB_USER}- The database user
116      * <LI>{@link #DB_PASSWORD}- The database password
117      * <LI>{@link #DB_MAX_CONNECTIONS}- The maximum # connections in the pool,
118      * optional
119      * <LI>{@link #DB_VALIDATION_QUERY}- The sql validation query, optional
120      * </UL>
121      * </p>
122      *
123      * @param config
124      * configuration properties
125      */

126     public PoolingConnectionProvider(Properties JavaDoc config) throws SQLException JavaDoc {
127         PropertiesParser cfg = new PropertiesParser(config);
128         initialize(
129             cfg.getStringProperty(DB_DRIVER),
130             cfg.getStringProperty(DB_URL),
131             cfg.getStringProperty(DB_USER, ""),
132             cfg.getStringProperty(DB_PASSWORD, ""),
133             cfg.getIntProperty(DB_MAX_CONNECTIONS, DEFAULT_DB_MAX_CONNECTIONS),
134             cfg.getStringProperty(DB_VALIDATION_QUERY));
135     }
136     
137     /*
138      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139      *
140      * Interface.
141      *
142      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143      */

144     
145     /**
146      * Create the underlying DBCP BasicDataSource with the
147      * default supported properties.
148      */

149     private void initialize(
150         String JavaDoc dbDriver,
151         String JavaDoc dbURL,
152         String JavaDoc dbUser,
153         String JavaDoc dbPassword,
154         int maxConnections,
155         String JavaDoc dbValidationQuery) throws SQLException JavaDoc {
156         if (dbURL == null) {
157             throw new SQLException JavaDoc(
158                 "DBPool could not be created: DB URL cannot be null");
159         }
160         
161         if (dbDriver == null) {
162             throw new SQLException JavaDoc(
163                 "DBPool '" + dbURL + "' could not be created: " +
164                 "DB driver class name cannot be null!");
165         }
166         
167         if (maxConnections < 0) {
168             throw new SQLException JavaDoc(
169                 "DBPool '" + dbURL + "' could not be created: " +
170                 "Max connections must be greater than zero!");
171         }
172
173         datasource = new BasicDataSource();
174         datasource.setDriverClassName(dbDriver);
175         datasource.setUrl(dbURL);
176         datasource.setUsername(dbUser);
177         datasource.setPassword(dbPassword);
178         datasource.setMaxActive(maxConnections);
179         if (dbValidationQuery != null) {
180             datasource.setValidationQuery(dbValidationQuery);
181         }
182     }
183     
184     /**
185      * Get the DBCP BasicDataSource created during initialization.
186      *
187      * <p>
188      * This can be used to set additional data source properties in a
189      * subclass's constructor.
190      * </p>
191      */

192     protected BasicDataSource getDataSource() {
193         return datasource;
194     }
195
196     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
197         return datasource.getConnection();
198     }
199     
200     public void shutdown() throws SQLException JavaDoc {
201         datasource.close();
202     }
203 }
204
Popular Tags