📄 dbconnectiondefaultpool.java
字号:
/**
* Copyright (C) 2001 Yasna.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* 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.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Yasna.com (http://www.yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Yazd" and "Yasna.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact yazd@yasna.com.
*
* 5. Products derived from this software may not be called "Yazd",
* nor may "Yazd" appear in their name, without prior written
* permission of Yasna.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 YASNA.COM OR
* ITS 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 software consists of voluntary contributions made by many
* individuals on behalf of Yasna.com. For more information
* on Yasna.com, please see <http://www.yasna.com>.
*/
/**
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* 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.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* CoolServlets.com (http://www.coolservlets.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Jive" and "CoolServlets.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@coolservlets.com.
*
* 5. Products derived from this software may not be called "Jive",
* nor may "Jive" appear in their name, without prior written
* permission of CoolServlets.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 COOLSERVLETS.COM OR
* ITS 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 software consists of voluntary contributions made by many
* individuals on behalf of CoolServlets.com. For more information
* on CoolServlets.com, please see <http://www.coolservlets.com>.
*/
package com.Yasna.forum.database;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.text.*;
import java.util.Date;
import com.Yasna.forum.*;
/**
* Default Yazd connection provider. It uses the excellent connection pool
* available from http://www.javaexchange.com. This connection provider is a
* a good choice unless you can use a container-managed one.
*/
public class DbConnectionDefaultPool extends DbConnectionProvider {
private static final String NAME = "Default Connection Pool";
private static final String DESCRIPTION = "The default connection provider "
+ "that uses the connection pool from javaexchange.com. It works with "
+ "almost any database setup, is customizable, and offers good performance. "
+ "Use this connection provider unless you have your own or can use a "
+ "container managed connection pool.";
private static final String AUTHOR = "Yazd.Yasna.com";
private static final int MAJOR_VERSION = 1;
private static final int MINOR_VERSION = 0;
private static final boolean POOLED = true;
private ConnectionPool connectionPool = null;
private Properties props;
private Properties propDescriptions;
private Object initLock = new Object();
public DbConnectionDefaultPool() {
//this.manager = manager;
props = new Properties();
propDescriptions = new Properties();
//Initialize all property values
initializeProperties();
//Load any existing property values
loadProperties();
}
/**
* Returns a database connection.
*/
public Connection getConnection() {
if (connectionPool == null) {
//block until the init has been done
synchronized(initLock) {
//if still null, something has gone wrong
if (connectionPool == null) {
System.err.println("Warning: DbConnectionDefaultPool.getConnection() was " +
"called when the internal pool has not been initialized.");
return null;
}
}
}
return new ConnectionWrapper(connectionPool.getConnection(), connectionPool);
}
/**
* Starts the pool.
*/
protected void start() {
//acquire lock so that no connections can be returned.
synchronized (initLock) {
//Get properties
String driver = props.getProperty("driver");
String server = props.getProperty("server");
String username = props.getProperty("username");
String password = props.getProperty("password");
int minConnections = 0, maxConnections = 0;
double connectionTimeout = 0.0;
try {
minConnections = Integer.parseInt(props.getProperty("minConnections"));
maxConnections = Integer.parseInt(props.getProperty("maxConnections"));
connectionTimeout = Double.parseDouble(props.getProperty("connectionTimeout"));
}
catch (Exception e) {
System.err.println("Error: could not parse default pool properties. " +
"Make sure the values exist and are correct.");
e.printStackTrace();
return;
}
String logPath = props.getProperty("logPath");
try {
connectionPool = new ConnectionPool(driver, server, username, password,
minConnections, maxConnections, logPath, connectionTimeout);
}
catch (IOException ioe) {
System.err.println("Error starting DbConnectionDefaultPool: " + ioe);
ioe.printStackTrace();
}
}
}
/**
* Restarts the pool to take into account any property changes.
*/
protected void restart() {
//Kill off pool.
destroy();
//Reload properties.
loadProperties();
//Start a new pool.
start();
}
/**
* Destroys the connection pool.
*/
protected void destroy() {
if (connectionPool != null) {
try {
connectionPool.destroy(1);
}
catch (Exception e) {
e.printStackTrace();
}
}
//Release reference to connectionPool
connectionPool = null;
}
/**
* Returns the value of a property of the connection provider.
*
* @param name the name of the property.
* @returns the value of the property.
*/
public String getProperty(String name) {
return (String)props.get(name);
}
/**
* Returns the description of a property of the connection provider.
*
* @param name the name of the property.
* @return the description of the property.
*/
public String getPropertyDescription(String name) {
return (String)propDescriptions.get(name);
}
/**
* Returns an enumeration of the property names for the connection provider.
*/
public Enumeration propertyNames() {
return props.propertyNames();
}
/**
* Sets a property of the connection provider. Each provider has a set number
* of properties that are determined by the author. Trying to set a non-
* existant property will result in an IllegalArgumentException.
*
* @param name the name of the property to set.
* @param value the new value for the property.
*
*/
public void setProperty(String name, String value) {
props.put(name, value);
saveProperties();
}
/**
* Give default values to all the properties and descriptions.
*/
private void initializeProperties() {
props.put("driver","");
props.put("server","");
props.put("username","");
props.put("password","");
props.put("minConnections","");
props.put("maxConnections","");
props.put("logPath","");
props.put("connectionTimeout","");
propDescriptions.put("driver","JDBC driver. e.g. 'oracle.jdbc.driver.OracleDriver'");
propDescriptions.put("server","JDBC connect string. e.g. 'jdbc:oracle:thin:@203.92.21.109:1526:orcl'");
propDescriptions.put("username","Database username. e.g. 'Scott'");
propDescriptions.put("password","Database password. e.g. 'Tiger'");
propDescriptions.put("minConnections","Minimum # of connections to start with in pool. Three is the recommended minimum");
propDescriptions.put("maxConnections","Maximum # of connections in dynamic pool. Fifteen should give good performance for an average load.");
propDescriptions.put("logPath","Absolute path name for log file. e.g. 'c:\\logs\\yazdDbLog.log'");
propDescriptions.put("connectionTimeout","Time in days between connection resets. e.g. '.5'");
}
/**
* Load whatever properties that already exist.
*/
private void loadProperties() {
String driver = PropertyManager.getProperty("DbConnectionDefaultPool.driver");
String server = PropertyManager.getProperty("DbConnectionDefaultPool.server");
String username = PropertyManager.getProperty("DbConnectionDefaultPool.username");
String password = PropertyManager.getProperty("DbConnectionDefaultPool.password");
String minConnections = PropertyManager.getProperty("DbConnectionDefaultPool.minConnections");
String maxConnections = PropertyManager.getProperty("DbConnectionDefaultPool.maxConnections");
String logPath = PropertyManager.getProperty("DbConnectionDefaultPool.logPath");
String connectionTimeout = PropertyManager.getProperty("DbConnectionDefaultPool.connectionTimeout");
if (driver != null) { props.setProperty("driver", driver); }
if (server != null) { props.setProperty("server", server); }
if (username != null) { props.setProperty("username", username); }
if (password != null) { props.setProperty("password", password); }
if (minConnections != null) { props.setProperty("minConnections", minConnections); }
if (maxConnections != null) { props.setProperty("maxConnections", maxConnections); }
if (logPath != null) { props.setProperty("logPath", logPath); }
if (connectionTimeout != null) { props.setProperty("connectionTimeout", connectionTimeout); }
}
private void saveProperties() {
PropertyManager.setProperty("DbConnectionDefaultPool.driver", props.getProperty("driver"));
PropertyManager.setProperty("DbConnectionDefaultPool.server", props.getProperty("server"));
PropertyManager.setProperty("DbConnectionDefaultPool.username", props.getProperty("username"));
PropertyManager.setProperty("DbConnectionDefaultPool.password", props.getProperty("password"));
PropertyManager.setProperty("DbConnectionDefaultPool.minConnections", props.getProperty("minConnections"));
PropertyManager.setProperty("DbConnectionDefaultPool.maxConnections", props.getProperty("maxConnections"));
PropertyManager.setProperty("DbConnectionDefaultPool.logPath", props.getProperty("logPath"));
PropertyManager.setProperty("DbConnectionDefaultPool.connectionTimeout", props.getProperty("connectionTimeout"));
}
/**
* DbConnectionBroker
* @version 1.0.11 12/7/99
* @author Marc A. Mnich
*
* ----------------------------------------
* Modified June 18, 2000 by Matt Tucker
* Changes:
* - New package name, class name to make it nice to embed as
* an internal class.
* - Source code reformatting.
* - Added more error handling code in constructor, createConn method
* so that more information is given to Yazd users.
* DbConnectionBroker rules! Download it from javaexchange.com
* ----------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -