⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbconnectionpool.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
 * Created on 11-Apr-2005
 *
 * TODO All
 */
package uk.ac.kent.dpa.mysql.init;

import java.util.*;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import ke.files.*;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDriver;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;

/**
 * @author  Brendan Berney, Stefan Boddy, James Cunningham
 * <p>
 * DbConnectionPool manages JDBC database connections, maintaining a 
 * pool of open connections thus decreasing the average time taken to 
 * get an open connection to the database.
 * </p>
 */

public class DbConnectionPool
{


	/**
	 * <p>Initialises this instance of the connection pool.</p>
	 * <p> Takes parameters relating to a database driver and connections as well as parameters stating the type of database
	 * in use and the number of retries that should be made in case of errors</p>
	 *
	 * @param driverNameIn        		The name of the JDBC database driver in use <i>eg. "sun.jdbc.odbc.JdbcOdbcDriver" (if using the built-in jdbc-odbc bridge driver)</i>
	 * @param driverDbNamePrefixIn    	The JDBC database driver prefix <i>eg. "jdbc:odbc:" (if using the built-in jdbc-odbc bridge driver)</i>
	 * @param databaseNameIn          	The name of the database to use <i>eg. "CSandStore"</i>
	 * @param dbUserNameIn        		The database user name <i>eg. "kernelUser"</i>
	 * @param dbPasswordIn        		The database user password.
	 * @param dbTypeIn            		A byte value indicating the database type in use. (Values may be 0 - standard SQL92, 1 - SQL Server, 2 - Oracle 7i or above).
	 * @param usePrepStmtIn       		A boolean value indicating whether or not the Kernel uses prepared statements (true if yes, false if no). Using prepared statements increases speed but may not be supported by all JDBC drivers.
	 * @param maxDbRetriesIn      		An integer value dictating the number of times a database write operation should be retried in case of failure
	 * @return 							void
	 * @exception 						DBConnectionPoolException if the JDBC driver cannot be found
	 */

    public DbConnectionPool() {
    }
    
    private DbConnectionPool(String driverNameIn,
                             String driverDbNamePrefixIn, 
                             String databaseNameIn, 
                             String dbUserNameIn, 
                             String dbPasswordIn, 
                             byte dbTypeIn, 
                             boolean usePrepStmtIn, 
                             int maxDbRetriesIn) throws DatabaseException
    {
        this.driverDbNamePrefix = driverDbNamePrefixIn;
        this.databaseName = databaseNameIn;
        this.dbUserName = dbUserNameIn;
        this.dbPassword = dbPasswordIn;
        this.maxDbRetries=maxDbRetriesIn;
        this.usePrepStmt = usePrepStmtIn;
        try
        {
            DriverManager.registerDriver((Driver)Class.forName(driverNameIn).newInstance());
            GenericObjectPool.Config config = new GenericObjectPool.Config();
            Properties props = new Properties();
            config.maxActive = new Integer(props.getProperty("maxActive", "30")).intValue();
            config.maxIdle = new Integer(props.getProperty("maxIdle", "3")).intValue();
            config.maxWait = new Integer(props.getProperty("maxWait", "5000")).intValue();
            config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
            config.testOnBorrow = new Boolean(props.getProperty("testOnBorrow", "true")).booleanValue();
            config.testOnReturn = new Boolean(props.getProperty("testOnReturn", "false")).booleanValue();
            config.timeBetweenEvictionRunsMillis = new Integer(props.getProperty("timeBetweenEvictionRunsMillis", "300000")).intValue();
            config.minEvictableIdleTimeMillis = new Integer(props.getProperty("minEvictableIdleTimeMillis", "28800000")).intValue();;
            config.testWhileIdle = new Boolean(props.getProperty("testWhileIdle", "false")).booleanValue();
            config.numTestsPerEvictionRun = new Integer(props.getProperty("numTestsPerEvictionRun", "3")).intValue();;
            ObjectPool connectionPool = new GenericObjectPool(null, config);
            ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(driverDbNamePrefix+databaseName,dbUserName,dbPassword);
            // TODO add validation to PoolableConnectionFactory constructor, currently turned off (null)
            PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
            PoolingDriver driver = new PoolingDriver();
            driver.registerPool("cxns",connectionPool);
        }
        catch (Exception cnfExp)
        {
            throw new DatabaseException("DBCConnectionPool init: Can't find driver "+driverNameIn+"!", cnfExp);
        }
    }

	
	/**
	 * @return
	 * @throws DatabaseException
	 */
	


	/**
	 * Static method to return a singleton instance of DbConnectionPool to all callers.
	 * This method passes the initialisation parameters to the constructor of the
	 * underlying instance.
	 *
	 * @param driverNameIn        		The name of the JDBC database driver in use <i>eg. "sun.jdbc.odbc.JdbcOdbcDriver" (if using the built-in jdbc-odbc bridge driver)</i>
	 * @param driverDbNamePrefixIn    	The JDBC database driver prefix <i>eg. "jdbc:odbc:" (if using the built-in jdbc-odbc bridge driver)</i>
	 * @param databaseNameIn          	The name of the database to use <i>eg. "CSandStore"</i>
	 * @param dbUserNameIn        		The database user name <i>eg. "kernelUser"</i>
	 * @param dbPasswordIn        		The database user password.
	 * @param dbTypeIn            		A byte value indicating the database type in use. (Values may be 0 - standard SQL92, 1 - SQL Server, 2 - Oracle 7i or above).
	 * @param usePrepStmtIn       		A boolean value indicating whether or not the Kernel uses prepared statements (true if yes, false if no). Using prepared statements increases speed but may not be supported by all JDBC drivers.
	 * @param maxDbRetriesIn      		An integer value dictating the number of times a database write operation should be retried in case of failure
	 * @return 							void
	 * @exception 						DBConnectionPoolException if the JDBC driver cannot be found
	 */
	public DbConnectionPool getInstance(String driverNameIn,
                                           String driverDbNamePrefixIn, 
                                           String databaseNameIn, 
                                           String dbUserNameIn, 
                                           String dbPasswordIn, 
                                           byte dbTypeIn, 
                                           boolean usePrepStmtIn, 
                                           int maxDbRetriesIn) throws DatabaseException
	{
		if(instance!=null) return instance;
		instance = new DbConnectionPool(driverNameIn, driverDbNamePrefixIn, databaseNameIn, dbUserNameIn, dbPasswordIn, dbTypeIn, usePrepStmtIn, maxDbRetriesIn);
		return instance;
	}

	/**
	 * <p>Gets a connection from the pool.</p>
	 * <p>If a connection is not available in the pool, a new one is created. The connection is then returned.</p>
	 *
	 * @return  						A java.sql.Connection to be used.
	 * @exception   					SQLException if the connection cannot be opened.
	 * @see java.sql.Connection
	 */
	
	public Connection getConnection() throws SQLException
	{
            return DriverManager.getConnection("jdbc:apache:commons:dbcp:cxns");
	}

	/**
	 * <p>Releases a used connection back into the pool.</p>
	 *
	 * @param dbCxn   					A java.sql.Connection to be released.
	 * @return void
	 * @see java.sql.Connection
	 */
	
	public void releaseConnection(Connection dbCxn)
	{
            try{dbCxn.close();}catch(Exception e){};
	}
	
	//------------------------------------
	
	private DbConnectionPool instance = null;
	private long expiryTime;
	private Hashtable lockedCxns, unlockedCxns;
	private String driverDbNamePrefix;
	private String databaseName;
	private String dbUserName;
	private String dbPassword;
	private Driver dbDriver;
	private byte dbType;
	private boolean usePrepStmt;
	private int maxDbRetries;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -