`

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() + ".");
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics