📄 poolingconnectionprovider.java
字号:
/* * Copyright James House (c) 2001-2004 * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * * This product includes software developed by the Apache Software Foundation * (http://www.apache.org/) */package org.quartz.utils;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import org.apache.commons.dbcp.BasicDataSource;/** * <p> * A <code>ConnectionProvider</code> implementation that creates it's own * pool of connections. * </p> * * <p> * This class uses <a href="http://jakarta.apache.org/commons/index.html">DBCP * </a>, an Apache-Jakarta-Commons product. * </p> * * @see DBConnectionManager * @see ConnectionProvider * * @author Sharada Jambula * @author James House * @author Mohammad Rezaei */public class PoolingConnectionProvider implements ConnectionProvider { /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constants. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ public static final String DB_PROPS_PREFIX = "org.quartz.db."; public static final String DB_JNDI_DATASOURCE_URL = "jndiURL"; // The JDBC database driver public static final String DB_DRIVER = "driver"; // The JDBC database URL public static final String DB_URL = "URL"; // The database user name public static final String DB_USER = "user"; // The database user password public static final String DB_PASSWORD = "password"; public static final String DB_MAX_CONNECTIONS = "maxConnections"; public static final String DB_VALIDATION_QUERY = "validationQuery"; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Data members. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private BasicDataSource datasource; /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Constructors. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ public PoolingConnectionProvider(String dbDriver, String dbURL, String dbUser, String dbPassword, int maxConnections, String dbValidationQuery) throws SQLException { initialize(dbDriver, dbURL, dbUser, dbPassword, maxConnections, dbValidationQuery); } /* * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * Interface. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ private void initialize(String dbDriver, String dbURL, String dbUser, String dbPassword, int maxConnections, String dbValidationQuery) throws SQLException { if (dbDriver == null) throw new SQLException("DB driver class name cannot be null!"); if (dbURL == null) throw new SQLException("DB URL cannot be null!"); if (maxConnections < 0) throw new SQLException( "Max connections must be greater than zero!"); datasource = new BasicDataSource(); datasource.setDriverClassName(dbDriver); datasource.setUrl(dbURL); datasource.setUsername(dbUser); datasource.setPassword(dbPassword); datasource.setMaxActive(maxConnections); if (dbValidationQuery != null) datasource.setValidationQuery(dbValidationQuery); } /** * <p> * Create a connection pool using the given properties. * </p> * * <p> * The properties passed should contain either * <UL> * <LI>JNDI DataSource URL {@link #DB_JNDI_DATASOURCE_URL} * </UL> * or * <UL> * <LI>{@link #DB_DRIVER}- The database driver class name * <LI>{@link #DB_URL}- The database URL * <LI>{@link #DB_USER}- The database user * <LI>{@link #DB_PASSWORD}- The database password * <LI>{@link #DB_MAX_CONNECTIONS}- The maximum # connections in the pool * </UL> * <P> * * @param config * configuration properties * @exception SQLException * if an error occurs */ public PoolingConnectionProvider(Properties config) throws SQLException { PropertiesParser cfg = new PropertiesParser(config); String url = config.getProperty(DB_URL); try { initialize(config.getProperty(DB_DRIVER), url, config .getProperty(DB_USER), config.getProperty(DB_PASSWORD), cfg .getIntProperty(DB_MAX_CONNECTIONS, 3), cfg .getStringProperty(DB_VALIDATION_QUERY)); } catch (Exception e) { throw new SQLException("DBPool '" + url + "' could not be created: " + e.toString()); } } public Connection getConnection() throws SQLException { return this.datasource.getConnection(); } public void shutdown() throws SQLException { this.datasource.close(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -