genericdatasource.java
来自「这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用s」· Java 代码 · 共 950 行 · 第 1/2 页
JAVA
950 行
/*
* $Header: /sfroot/cvs/esimple/src/core/org/apache/struts/legacy/GenericDataSource.java,v 1.1.1.1 2004/09/08 06:38:35 lava Exp $
* $Revision: 1.1.1.1 $
* $Date: 2004/09/08 06:38:35 $
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. 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.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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 THE APACHE SOFTWARE FOUNDATION 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 the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.struts.legacy;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* <p>Generic data source implementation of the <code>DataSource</code>
* interface. <b>WARNING</b> - This implementation does not know how to
* provide connections with different username/password combinations.
* Calling this version of the implementation using the
* getConnection(username,password) signature will throw an exception.</p>
*
* <p>The following properties are supported by the standard
* <code>GenericDataSource</code> implementation:</p>
* <table>
* <tr>
* <th width="15%">Property</th>
* <th width="85%">Description</th>
* </tr>
* <tr>
* <td align="center">autoCommit</td>
* <td>Set to <code>true</code> if you want the connections returned to you
* by calling <code>getConnection()</code> to be configured in
* "auto-commit after every statement" mode. The default value is
* <code>true</code>, to conform to JDBC standard conventions.</td>
* </tr>
* <tr>
* <td align="center">description</td>
* <td>A short textual description of this data source. This property is
* required by the <code>javax.sql.DataSource</code> interface, but is
* not used within this implementation.</td>
* </tr>
* <tr>
* <td align="center">driverClass</td>
* <td>The fully qualified class name of the JDBC driver to be utilized for
* the connections created by this data source. Consult the
* documentation for your JDBC driver to identify the value to be
* configured for this property.</td>
* </tr>
* <tr>
* <td align="center">maxCount</td>
* <td>The maximum number of JDBC connections that will be created by this
* data source. This value must be greater than or equal to the value
* specified for the <code>minCount</count> property.</td>
* </tr>
* <tr>
* <td align="center">minCount</td>
* <td>The minimum number of JDBC connections to establish when this data
* source is first opened. This value must be less than or equal to the
* value specified for the <code>maxCount</code> property.</td>
* </tr>
* <tr>
* <td align="center">password</td>
* <td>The database password used to establish the connections created by
* this connection pool, in conjunction with the username specified in
* the <code>user</code> property.</td>
* </tr>
* <tr>
* <td align="center">pingCommand</td>
* <td>A non-query SQL command that, if specified, will be executed before
* a connection is returned by a call to <code>getConnection()</code>.
* If any SQLException is thrown by the execution of this statement,
* it is assumed that this connection is stale and it will be discarded.
* Because this happens on every connection allocation, you should ensure
* that the statement executes very quickly.</td>
* </tr>
* <tr>
* <td align="center">pingQuery</td>
* <td>A query SQL command (i.e. a SELECT) that, if specified, will be
* executed before a connection is returned by a call to
* <code>getConnection()</code>. If any SQLException is thrown by the
* execution of this query (or by the subsequent processing of the
* entire returned <code>ResultSet</code>), it is assumed that this
* connection is stale and it will be discarded. Because this happens
* on every connection allocation, you should ensure that the
* statement executes very quickly.</td>
* </tr>
* <tr>
* <td align="center">readOnly</td>
* <td>Set to <code>true</code> if you want the connections returned to you
* by calling <code>getConnection()</code> to be configured for read only
* operations. This can result in more efficient database access,
* because the database will know it does not need to retain undo logs
* for rolling back the transaction. The default value is
* <code>false</code>.</td>
* </tr>
* <tr>
* <td align="center">url</td>
* <td>The connection URL to be passed to our JDBC driver when establishing
* a new connection. The value specified typically starts with
* <code>jdbc:</code>, and includes a reference to the host (and,
* optionally, the port number) at which the database server is listening
* for connections, plus the name of the database to be opened. Consult
* the documentation for your JDBC driver to identify the value to be
* configured for this property.</td>
* </tr>
* <tr>
* <td align="center">user</td>
* <td>The database username used to establish the connections created by
* this connection pool, in conjunction with the password specified in
* the <code>password</code> property.</td>
* </tr>
* </table>
*
* <p>In addition, you can add to the set of <code>Properties</code> passed to
* the JDBC driver by calling <code>addProperty()</code>.</p>
* <p>
* This class was originally maintained in the core Struts util package [revision 1.11].
* </p>
*
* @author Craig R. McClanahan
* @author Ted Husted
* @version $Revision: 1.1.1.1 $ $Date: 2004/09/08 06:38:35 $
*/
public class GenericDataSource implements DataSource {
// ----------------------------------------------------------- Util Methods
/**
* Return the <code>Class</code> object for the specified fully qualified
* class name, from this web application's class loader.
* <p>
* See util.RequestUtils.applicationClass for maintained original.
*
* @param className Fully qualified class name to be loaded
* @return Class object
* @exception ClassNotFoundException if the class cannot be found
*/
private static Class applicationClass(String className) throws ClassNotFoundException {
// Look up the class loader to be used
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if (classLoader == null) {
classLoader = GenericDataSource.class.getClassLoader();
}
// Attempt to load the specified class
return (classLoader.loadClass(className));
}
/**
* Return a new instance of the specified fully qualified class name,
* after loading the class from this web application's class loader.
* The specified class <strong>MUST</strong> have a public zero-arguments
* constructor.
* <p>
* See util.RequestUtils.applicationClass for maintained original.
*
* @param className Fully qualified class name to use
* @return new instance of class
* @exception ClassNotFoundException if the class cannot be found
* @exception IllegalAccessException if the class or its constructor
* is not accessible
* @exception InstantiationException if this class represents an
* abstract class, an interface, an array class, a primitive type,
* or void
* @exception InstantiationException if this class has no
* zero-arguments constructor
*/
private static Object applicationInstance(String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException {
return (applicationClass(className).newInstance());
}
// ----------------------------------------------------- Instance Constants
private static final String SQLEXCEPTION_GETCONNECTION =
"getConnection(String username, String password) Method not supported. Use getConnection() instead.";
// ----------------------------------------------------- Instance Variables
/**
* Has this data source been closed?
*/
protected boolean closed = false;
/**
* The list of Connections (wrapped in our associated wrapper class) that
* have been created but are not currently in use.
*/
protected LinkedList connections = new LinkedList();
/**
* The JDBC driver that we use as a connection factory.
*/
protected Driver driver = null;
/**
* Commons Logging instance.
*/
protected Log log = LogFactory.getLog(this.getClass());
/**
* The login timeout for this data source.
*/
protected int loginTimeout = 0;
/**
* The log writer for this data source.
*/
protected PrintWriter logWriter = null;
// ------------------------------------------------------------- Properties
/**
* Add a generic property to the list of connection properties to be used.
*
* @param name Name of the generic property
* @param value Corresponding generic property value
*/
public void addProperty(String name, String value) {
properties.put(name, value);
}
/**
* The number of connections that have been created by this data source.
*/
protected int activeCount = 0;
public int getActiveCount() {
return (this.activeCount);
}
/**
* The default auto-commit state for newly created connections.
*/
protected boolean autoCommit = true;
public boolean getAutoCommit() {
return (this.autoCommit);
}
public void setAutoCommit(boolean autoCommit) {
this.autoCommit = autoCommit;
}
/**
* The debugging detail level for this data source.
*/
protected int debug = 0;
public int getDebug() {
return (this.debug);
}
public void setDebug(int debug) {
this.debug = debug;
}
/**
* The description of this data source.
*/
protected String description = null;
public String getDescription() {
return (this.description);
}
public void setDescription(String description) {
this.description = description;
}
/**
* The Java class name of the JDBC driver to use.
*/
protected String driverClass = null;
public String getDriverClass() {
return (this.driverClass);
}
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
/**
* The maximum number of connections to be created.
*/
protected int maxCount = 2;
public int getMaxCount() {
return (this.maxCount);
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
/**
* The minimum number of connections to be created.
*/
protected int minCount = 1;
public int getMinCount() {
return (this.minCount);
}
public void setMinCount(int minCount) {
this.minCount = minCount;
}
/**
* The database password for use in establishing a connection.
*/
protected String password = null;
public String getPassword() {
return (this.password);
}
public void setPassword(String password) {
this.password = password;
addProperty("password", this.password);
}
/**
* The non-query SQL command used to ping an allocated connection.
*/
protected String pingCommand = null;
public String getPingCommand() {
return (this.pingCommand);
}
public void setPingCommand(String pingCommand) {
this.pingCommand = pingCommand;
}
/**
* The query SQL command used to ping an allocated connection.
*/
protected String pingQuery = null;
public String getPingQuery() {
return (this.pingQuery);
}
public void setPingQuery(String pingQuery) {
this.pingQuery = pingQuery;
}
/**
* The connection properties for use in establishing connections.
*/
protected Properties properties = new Properties();
/**
* The default read-only state for newly created connections.
*/
protected boolean readOnly = false;
public boolean getReadOnly() {
return (this.readOnly);
}
public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
}
/**
* The JDBC URL for the database connection to be opened.
*/
protected String url = null;
public String getUrl() {
return (this.url);
}
public void setUrl(String url) {
this.url = url;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?