001: /*
002: * Copyright 2004-2005 OpenSymphony
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy
006: * of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations
014: * under the License.
015: *
016: */
017:
018: /*
019: * Previously Copyright (c) 2001-2004 James House
020: */
021: package org.quartz.utils;
022:
023: import java.sql.Connection;
024: import java.sql.SQLException;
025: import java.util.Properties;
026:
027: import org.apache.commons.dbcp.BasicDataSource;
028:
029: /**
030: * <p>
031: * A <code>ConnectionProvider</code> implementation that creates its own
032: * pool of connections.
033: * </p>
034: *
035: * <p>
036: * This class uses <a href="http://jakarta.apache.org/commons/dbcp/">DBCP</a>,
037: * an Apache-Jakarta-Commons product.
038: * </p>
039: *
040: * @see DBConnectionManager
041: * @see ConnectionProvider
042: *
043: * @author Sharada Jambula
044: * @author James House
045: * @author Mohammad Rezaei
046: */
047: public class PoolingConnectionProvider implements ConnectionProvider {
048:
049: /*
050: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
051: *
052: * Constants.
053: *
054: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
055: */
056:
057: /** The JDBC database driver. */
058: public static final String DB_DRIVER = "driver";
059:
060: /** The JDBC database URL. */
061: public static final String DB_URL = "URL";
062:
063: /** The database user name. */
064: public static final String DB_USER = "user";
065:
066: /** The database user password. */
067: public static final String DB_PASSWORD = "password";
068:
069: /** The maximum number of database connections to have in the pool. */
070: public static final String DB_MAX_CONNECTIONS = "maxConnections";
071:
072: /**
073: * The database sql query to execute everytime a connection is retrieved
074: * from the pool to ensure that it is still valid.
075: */
076: public static final String DB_VALIDATION_QUERY = "validationQuery";
077:
078: /** Default maximum number of database connections in the pool. */
079: public static final int DEFAULT_DB_MAX_CONNECTIONS = 10;
080:
081: /*
082: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
083: *
084: * Data members.
085: *
086: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
087: */
088:
089: private BasicDataSource datasource;
090:
091: /*
092: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
093: *
094: * Constructors.
095: *
096: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
097: */
098:
099: public PoolingConnectionProvider(String dbDriver, String dbURL,
100: String dbUser, String dbPassword, int maxConnections,
101: String dbValidationQuery) throws SQLException {
102: initialize(dbDriver, dbURL, dbUser, dbPassword, maxConnections,
103: dbValidationQuery);
104: }
105:
106: /**
107: * Create a connection pool using the given properties.
108: *
109: * <p>
110: * The properties passed should contain:
111: * <UL>
112: * <LI>{@link #DB_DRIVER}- The database driver class name
113: * <LI>{@link #DB_URL}- The database URL
114: * <LI>{@link #DB_USER}- The database user
115: * <LI>{@link #DB_PASSWORD}- The database password
116: * <LI>{@link #DB_MAX_CONNECTIONS}- The maximum # connections in the pool,
117: * optional
118: * <LI>{@link #DB_VALIDATION_QUERY}- The sql validation query, optional
119: * </UL>
120: * </p>
121: *
122: * @param config
123: * configuration properties
124: */
125: public PoolingConnectionProvider(Properties config)
126: throws SQLException {
127: PropertiesParser cfg = new PropertiesParser(config);
128: initialize(cfg.getStringProperty(DB_DRIVER), cfg
129: .getStringProperty(DB_URL), cfg.getStringProperty(
130: DB_USER, ""), cfg.getStringProperty(DB_PASSWORD, ""),
131: cfg.getIntProperty(DB_MAX_CONNECTIONS,
132: DEFAULT_DB_MAX_CONNECTIONS), cfg
133: .getStringProperty(DB_VALIDATION_QUERY));
134: }
135:
136: /*
137: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138: *
139: * Interface.
140: *
141: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142: */
143:
144: /**
145: * Create the underlying DBCP BasicDataSource with the
146: * default supported properties.
147: */
148: private void initialize(String dbDriver, String dbURL,
149: String dbUser, String dbPassword, int maxConnections,
150: String dbValidationQuery) throws SQLException {
151: if (dbURL == null) {
152: throw new SQLException(
153: "DBPool could not be created: DB URL cannot be null");
154: }
155:
156: if (dbDriver == null) {
157: throw new SQLException("DBPool '" + dbURL
158: + "' could not be created: "
159: + "DB driver class name cannot be null!");
160: }
161:
162: if (maxConnections < 0) {
163: throw new SQLException("DBPool '" + dbURL
164: + "' could not be created: "
165: + "Max connections must be greater than zero!");
166: }
167:
168: datasource = new BasicDataSource();
169: datasource.setDriverClassName(dbDriver);
170: datasource.setUrl(dbURL);
171: datasource.setUsername(dbUser);
172: datasource.setPassword(dbPassword);
173: datasource.setMaxActive(maxConnections);
174: if (dbValidationQuery != null) {
175: datasource.setValidationQuery(dbValidationQuery);
176: }
177: }
178:
179: /**
180: * Get the DBCP BasicDataSource created during initialization.
181: *
182: * <p>
183: * This can be used to set additional data source properties in a
184: * subclass's constructor.
185: * </p>
186: */
187: protected BasicDataSource getDataSource() {
188: return datasource;
189: }
190:
191: public Connection getConnection() throws SQLException {
192: return datasource.getConnection();
193: }
194:
195: public void shutdown() throws SQLException {
196: datasource.close();
197: }
198: }
|