📄 cmsdbpool.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsDbPool.java,v $
* Date : $Date: 2006/10/04 15:09:43 $
* Version: $Revision: 1.46 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software GmbH, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.db;
import org.opencms.main.CmsLog;
import org.opencms.util.CmsStringUtil;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.ExtendedProperties;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
/**
* Various methods to create DBCP pools.<p>
*
* Only JDBC Driver based pools are supported currently. JNDI DataSource
* based pools might be added probably later.<p>
*
* <b>Please note:</b> This class is subject to change in later versions.
* To obtain information about the connections, please use the
* {@link org.opencms.db.CmsSqlManager}.<p>
*
* @author Thomas Weckert
*
* @version $Revision: 1.46 $
*
* @since 6.0.0
*/
public final class CmsDbPool {
/** This prefix is required to make the JDBC DriverManager return pooled DBCP connections. */
public static final String DBCP_JDBC_URL_PREFIX = "jdbc:apache:commons:dbcp:";
/** Prefix for database keys. */
public static final String KEY_DATABASE = "db.";
/** Key for the database name. */
public static final String KEY_DATABASE_NAME = KEY_DATABASE + "name";
/** Key for the pool id. */
public static final String KEY_DATABASE_POOL = KEY_DATABASE + "pool";
/** Key for statement pooling. */
public static final String KEY_DATABASE_STATEMENTS = KEY_DATABASE + "statements";
/** Key for jdbc driver. */
public static final String KEY_JDBC_DRIVER = "jdbcDriver";
/** Key for jdbc url. */
public static final String KEY_JDBC_URL = "jdbcUrl";
/** Key for jdbc url params. */
public static final String KEY_JDBC_URL_PARAMS = KEY_JDBC_URL + ".params";
/** Key for maximum active connections. */
public static final String KEY_MAX_ACTIVE = "maxActive";
/** Key for maximum idle connections. */
public static final String KEY_MAX_IDLE = "maxIdle";
/** Key for maximum wait time. */
public static final String KEY_MAX_WAIT = "maxWait";
/** Key for minimum idle time before a connection is subject to an eviction test. */
public static final String KEY_MIN_EVICTABLE_IDLE_TIME = "minEvictableIdleTime";
/** Key for minimum number of connections kept open. */
public static final String KEY_MIN_IDLE = "minIdle";
/** Key for number of tested connections per run. */
public static final String KEY_NUM_TESTS_PER_EVICTION_RUN = "numTestsPerEvictionRun";
/** Key for database password. */
public static final String KEY_PASSWORD = "password";
/** Key for default. */
public static final String KEY_POOL_DEFAULT = "default";
/** Key for pool url. */
public static final String KEY_POOL_URL = "poolUrl";
/** Key for pool user. */
public static final String KEY_POOL_USER = "user";
/** Key for vfs pool. */
public static final String KEY_POOL_VFS = "vfs";
/** Key for pooling flag. */
public static final String KEY_POOLING = "pooling";
/** Key for test on borrow flag. */
public static final String KEY_TEST_ON_BORROW = "testOnBorrow";
/** Key for test query. */
public static final String KEY_TEST_QUERY = "testQuery";
/** Key for test while idle flag. */
public static final String KEY_TEST_WHILE_IDLE = "testWhileIdle";
/** Key for time between two eviction runs. */
public static final String KEY_TIME_BETWEEN_EVICTION_RUNS = "timeBetweenEvictionRuns";
/** Key for user name. */
public static final String KEY_USERNAME = "user";
/** Key for "when pool exhausted" action. */
public static final String KEY_WHEN_EXHAUSTED_ACTION = "whenExhaustedAction";
/** The name of the opencms default pool. */
public static final String OPENCMS_DEFAULT_POOL_NAME = "default";
/** The default OpenCms JDBC pool URL. */
public static final String OPENCMS_DEFAULT_POOL_URL = "opencms:default";
/** The prefix used for opencms JDBC pools. */
public static final String OPENCMS_URL_PREFIX = "opencms:";
/**
* Default constructor.<p>
*
* Nobody is allowed to create an instance of this class!
*/
private CmsDbPool() {
super();
}
/**
* Creates a JDBC DriverManager based DBCP connection pool.<p>
*
* @param configuration the configuration (opencms.properties)
* @param key the key of the database pool in the configuration
* @return String the URL to access the created DBCP pool
* @throws Exception if the pool could not be initialized
*/
public static PoolingDriver createDriverManagerConnectionPool(Map configuration, String key) throws Exception {
ExtendedProperties config;
if (configuration instanceof ExtendedProperties) {
config = (ExtendedProperties)configuration;
} else {
config = new ExtendedProperties();
config.putAll(configuration);
}
// read the values of the pool configuration specified by the given key
String jdbcDriver = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_DRIVER);
String jdbcUrl = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL);
String jdbcUrlParams = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL_PARAMS);
int maxActive = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_ACTIVE, 10);
int maxWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_WAIT, 2000);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -