`

PooledConnectionManager

    博客分类:
  • Java
 
阅读更多
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import oracle.jdbc.OracleDriver;
import oracle.jdbc.pool.OracleOCIConnectionPool;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.logging.LogUtil;
import com.common.Constants;
import com.common.Util;
import com.ui.AbstractPlugin;

/**
 * PooledConnectionManager *
 */
public class PooledConnectionManager {

    /**
     * String decryptFileName
     */
    private String decryptFileName = "OCIFileName";
    /**
     * PooledConnectionManager instance
     */
    private static PooledConnectionManager instance = null;
    /**
     * Map poolMap
     */
    private static Map poolMap = null;
    /**
     * String defaultPoolName
     */
    private static String defaultPoolName = null;
    /**
     * String debugInfoFlag
     */
    private static String debugInfoFlag = "false";
    
    /**
     * PooledConnectionManager
     * @return instance
     * @throws Exception
     */
    public static PooledConnectionManager getInstance() throws Exception {
        if(instance == null) {
            instance = new PooledConnectionManager();
        }
        return instance;
    }
    
    /**
     * PooledConnectionManager
     * @throws Exception
     */
    protected PooledConnectionManager() throws Exception {
        if (poolMap == null) {
            poolMap = new HashMap();
            SAXReader reader = new SAXReader();
            Document document = reader.read(AbstractPlugin.class.getResourceAsStream(Constants.CONFIG_COMMON_PATH));
            Element ociPoolsEle = (Element) document.getRootElement().element(Constants.OCIPOOLS);
            if("true".equals(ociPoolsEle.attributeValue("debugInfo"))) {
                debugInfoFlag = "true";
            }
            //Get the name of decryptConsole
            String decryptConsole = ociPoolsEle.elementTextTrim(Constants.OCIPOOL_DECRYPT_CONSOLE);
            Iterator iter = ociPoolsEle.elementIterator("OCIPool");
            DriverManager.registerDriver(new OracleDriver());
            while (iter.hasNext()) {
                Element recordEle = (Element) iter.next();
                if("true".equals(recordEle.attributeValue("default"))) {
                    defaultPoolName = recordEle.attributeValue("name");
                }
                Properties poolPro = new Properties();
                poolPro.put(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT, recordEle.elementTextTrim(Constants.OCIPOOL_MINLIMIT_PRO));
                poolPro.put(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT, recordEle.elementTextTrim(Constants.OCIPOOL_MAXLIMIT_PRO));
                poolPro.put(OracleOCIConnectionPool.CONNPOOL_INCREMENT, recordEle.elementTextTrim(Constants.OCIPOOL_INCREMENT_PRO));
                poolPro.put(OracleOCIConnectionPool.CONNPOOL_TIMEOUT, recordEle.elementTextTrim(Constants.OCIPOOL_CONIDLETIME_PRO));
                //Decoding the password
                String password = Util.decrypt(recordEle.elementTextTrim(Constants.OCIPOOL_PASSWORD_PRO), decryptFileName, decryptConsole);
                OracleOCIConnectionPool cpool = new OracleOCIConnectionPool(recordEle.elementTextTrim(Constants.OCIPOOL_USERNAME_PRO), 
                                                                            password, 
                                                                            recordEle.elementTextTrim(Constants.OCIPOOL_URL_PRO), 
                                                                            poolPro);
                poolMap.put(recordEle.attributeValue("name"), cpool);
            }
        }
    }
    
    /**
     * OracleOCIConnectionPool
     * @param poolName
     * @return cpool
     * @throws Exception
     */
    public OracleOCIConnectionPool getConnectionPool(String poolName) throws Exception {
        OracleOCIConnectionPool cpool = (OracleOCIConnectionPool)poolMap.get(poolName);
        if(cpool == null) {
            throw new NoSuchPoolException("There is no OCI pool named \"" + poolName + "\".");
        }
        if(debugInfoFlag.equals("true")) {
            this.printPoolInfo(poolName);
        }
        return cpool;
    }
    
    /**
     * OracleOCIConnectionPool
     * @return cpool
     * @throws Exception
     */
    public OracleOCIConnectionPool getConnectionPool() throws Exception {
        OracleOCIConnectionPool cpool = null;
        if(defaultPoolName != null) {
            //Get the default pool which is assigned.
            cpool = (OracleOCIConnectionPool)poolMap.get(defaultPoolName);
            if(debugInfoFlag.equals("true")) {
                this.printPoolInfo(defaultPoolName);
            }
        } else {
            //If there is no default pool to be assigned,get the first pool.
            if(poolMap.keySet() != null) {
                cpool = (OracleOCIConnectionPool)poolMap.get(poolMap.keySet().toArray()[0]);
                if(debugInfoFlag.equals("true")) {
                    this.printPoolInfo((String)poolMap.keySet().toArray()[0]);
                }
            }
        }
        return cpool;
    }

    /**
     * getConnection
     * @param poolName
     * @return conn
     * @throws Exception
     */
    public Connection getConnection(String poolName) throws Exception {
        return this.getConnectionPool(poolName).getConnection();
    }
    
    /**
     * getConnection
     * @return conn
     * @throws Exception
     */
    public Connection getConnection() throws Exception {
        OracleOCIConnectionPool cpool = this.getConnectionPool();
        if(cpool != null) {
            return cpool.getConnection();
        }
        return null;
    }
    
    /**
     * printPoolInfo
     * @param poolName
     * @throws Exception
     */
    private void printPoolInfo(String poolName) throws Exception {
        OracleOCIConnectionPool cpool = (OracleOCIConnectionPool)poolMap.get(poolName);
        LogUtil.info(this.getClass(), "Current OCI connection pool is: " + poolName + ".");
        LogUtil.info(this.getClass(), "Pool size is: " + cpool.getPoolSize() + ".");
        LogUtil.info(this.getClass(), "Active size is: " + cpool.getActiveSize() + ".");
        LogUtil.info(this.getClass(), "Min pool size limit is: " + cpool.getMinLimit() + ".");
        LogUtil.info(this.getClass(), "Max pool size limit is: " + cpool.getMaxLimit() + ".");
    }
}

 

分享到:
评论

相关推荐

    c3p0连接池源码

    你可以查看`com.mchange.v2.c3p0`包下的类,如`CommingledPool`、`PooledConnectionManager`和`ManagedConnectionFactory`等,了解它们在连接池管理中的角色。 `c3p0-0.9.1.2.bin`可能包含编译后的库文件或者二进制...

    c3p0源码包

    3. **连接生命周期管理**:关注`com.mchange.v2.c3p0.impl`包内的`PooledConnectionManager`和`PoolBackedDataSource`等类,理解连接的创建、分配、回收和关闭流程。 4. **异常处理机制**:查看`...

    c3p0连接池与源码

    源码中的关键类如`PooledConnectionManager`、`PoolableConnectionFactory`和`PoolConfig`分别对应连接管理、池化连接工厂和配置信息。 **五、最佳实践** 1. **合理设置参数**: 根据应用的并发量和数据库性能调整...

    c3p0jar包和源码

    例如,`PooledConnectionManager`类负责管理所有连接,`PoolableConnection`类代表池中的一个连接实例,`Combiner`类用于合并多个连接池的配置等。 **五、应用场景** c3p0常用于中小型的Java Web应用,与Spring等...

    数据库连接池 C3P0源码

    - **PooledConnectionManager**: 连接管理器,负责创建、维护和销毁数据库连接,以及连接池的初始化和清理工作。 - **PoolConfig**: 存储所有配置属性的类,例如最大连接数、最小连接数、超时时间等。 - **TestKit...

Global site tag (gtag.js) - Google Analytics