亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? dbconnectionmanager.java

?? java servlet著名論壇源代碼
?? JAVA
字號:
/*
 * $Header: /cvsroot/mvnforum/myvietnam/src/net/myvietnam/mvncore/db/DBConnectionManager.java,v 1.10 2004/01/18 19:06:43 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.10 $
 * $Date: 2004/01/18 19:06:43 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2004 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding MyVietnam and MyVietnam CoreLib
 * MUST remain intact in the scripts and source code.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 */
package net.myvietnam.mvncore.db;

import java.sql.*;
import java.util.Vector;
import java.util.Enumeration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class is a Singleton that provides access to the
 * connection pool. A client gets access to the single
 * instance through the static getInstance() method
 * and can then check-out and check-in connections from a pool.
 * When the client shuts down it should call the release() method
 * to close all opened connections and do other clean up.
 */
class DBConnectionManager {

    private static Log log = LogFactory.getLog(DBConnectionManager.class);

    private static final int TIME_BETWEEN_RETRIES = 500; // O.5 second

    // static variable
    static private DBConnectionManager instance = null;       // The single instance

    // instance variable
    private DBConnectionPool pool = null;// please be careful if u want to make this variable static

    /**
     * A private constructor since this is a Singleton
     * Note: This constructor is lightweight since DBConnectionPool is lightweight,
     *       so no connection is created until the first time getConnection() is called
     */
    private DBConnectionManager(DBOptions option) {
        try {
            Class.forName(option.driverClassName).newInstance();
        } catch (Exception e) {
            log.fatal("DBConnectionManager: Unable to load driver = " + option.driverClassName);
        }

        //if (pool == null) {//uncomment since pool is an instance variable
        pool = new DBConnectionPool(option.databaseURL, option.databaseUser, option.databasePassword, option.maxConnection);
        //}
    }

    /**
     * Returns the single instance, creating one if it's the
     * first time this method is called.
     *
     * @return DBConnectionManager The single instance.
     */
    static synchronized public DBConnectionManager getInstance() {
        if (instance == null) {
            DBOptions option = new DBOptions();
            instance = new DBConnectionManager(option);
        }
        return instance;
    }

    /**
     * Returns the single instance, creating one if it's the
     * first time this method is called.
     *
     * @return DBConnectionManager The single instance.
     */
    static synchronized public DBConnectionManager getInstance(DBOptions option) {
        if (instance == null) {
            if (option == null) {
                option = new DBOptions();
            }
            instance = new DBConnectionManager(option);
        }
        return instance;
    }

    /**
     * Returns a connection to the pool.
     *
     * @param con The Connection
     */
    void freeConnection(Connection con) {
        pool.freeConnection(con);
    }

    /**
     * Returns an open connection. If no one is available, and the max
     * number of connections has not been reached, a new connection is
     * created.
     *
     * @return Connection The connection or null
     */
    Connection getConnection() {
        return pool.getConnection();
    }

    /**
     * Returns an open connection. If no one is available, and the max
     * number of connections has not been reached, a new connection is
     * created. If the max number has been reached, waits until one
     * is available or the specified time has elapsed.
     *
     * @param time The number of milliseconds to wait
     * @return Connection The connection or null
     */
    Connection getConnection(long time) {
        return pool.getConnection(time);
    }

    /**
     * Closes all open connections.
     * @return true if the pool is empty and balance
     *         false if the pool has returned some connection to outside
     */
    boolean release() {
        return pool.release();
    }

    /**
     * This inner class represents a connection pool. It creates new
     * connections on demand, up to a max number if specified.
     * It also checks to make sure that the connection is still open
     * before it is returned to a client.
     */
    class DBConnectionPool {
        private int checkedOut  = 0;//NOTE: this variable should be changed in synchronized method only
        private Vector freeConnections = new Vector();

        private int    maxConn  = 0;
        private String password = null;
        private String URL      = null;
        private String user     = null;

        /**
         * Creates new connection pool.
         * NOTE: new an instance of this class is lightweight since it does not create any connections
         *
         * @param URL The JDBC URL for the database
         * @param user The database user, or null
         * @param password The database user password, or null
         * @param maxConn The maximal number of connections, or 0 for no limit
         */
        public DBConnectionPool(String URL, String user, String password, int maxConn) {
            this.URL = URL;
            this.user = user;
            this.password = password;
            this.maxConn = maxConn;
        }

        /**
         * Checks in a connection to the pool. Notify other Threads that
         * may be waiting for a connection.
         *
         * @todo: Maybe we dont need notifyAll(); ???
         *
         * @param con The connection to check in
         */
        synchronized void freeConnection(Connection con) {
            // Put the connection at the end of the Vector
            if (con != null) {//make sure that the connection is not null
                if (checkedOut <= 0) {
                    // this means that connection is open too much
                    // There are 2 cases:
                    // 1. Not get from this connection pool (maybe get directly)
                    // 2. this connection is gotten and then the whole pool is released
                    // In these case, just close the connection
                    try {
                        log.debug("DBConnectionManager: about to close the orphan connection.");
                        con.close();
                    } catch (SQLException ex) { }
                } else {
                    // Return this connection to the pool
                    // note that we dont have to check if the connection is not connected
                    // this will be check in the getConnection method
                    freeConnections.addElement(con);
                    // FIXME: posible negative value
                    // NOTE: checkOut should never be negative here
                    checkedOut--; // NOTE: this number can be negative (in case connection does not come from the pool)
                    notifyAll(); // can I remove it ???
                }
            }
        }

        /**
         * Checks out a connection from the pool. If no free connection
         * is available, a new connection is created unless the max
         * number of connections has been reached. If a free connection
         * has been closed by the database, it's removed from the pool
         * and this method is called again recursively.
         */
        synchronized Connection getConnection() {
            Connection con = null;

            while ( (freeConnections.size() > 0) && (con == null) ) {
                // Pick the first Connection in the Vector
                // to get round-robin usage
                con = (Connection) freeConnections.firstElement();
                freeConnections.removeElementAt(0);
                try {
                    if (con.isClosed()) {
                        log.info("Removed bad connection in DBConnectionPool.");
                        con = null; // to make the while loop to continue
                    }
                } catch (SQLException e) {
                    con = null; // to make the while loop to continue
                }
            } // while

            if (con == null) {// cannot get any connection from the pool
                if (maxConn == 0 || checkedOut < maxConn) {// maxConn = 0 means unlimited connections
                    con = newConnection();
                }
            }
            if (con != null) {
                checkedOut++;
            }
            return con;
        }

        /**
         * Checks out a connection from the pool. If no free connection
         * is available, a new connection is created unless the max
         * number of connections has been reached. If a free connection
         * has been closed by the database, it's removed from the pool
         * and this method is called again recursively.
         * <P>
         * If no connection is available and the max number has been
         * reached, this method waits the specified time for one to be
         * checked in.
         *
         * @param timeout The timeout value in milliseconds
         */
        /**
         * Note that this method is not synchronized since it relies on the getConnection(void) method
         * I also believe that this method SHOULD NOT synchronized because I use #sleep() method
         * @todo: check if we should synchronize this method and use wait instead of sleep ???
         */
        Connection getConnection(long timeout) {
            long startTime = System.currentTimeMillis();
            Connection con;
            while ((con = getConnection()) == null) {
                long elapsedTime = System.currentTimeMillis() - startTime;
                if (elapsedTime >= timeout) {
                    // Timeout has expired
                    return null;
                }

                long timeToWait = timeout - elapsedTime;
                if (timeToWait > TIME_BETWEEN_RETRIES) timeToWait = TIME_BETWEEN_RETRIES;// we dont want to wait for more than TIME_BETWEEN_RETRIES second each time
                try {
                    Thread.sleep(timeToWait);
                } catch (InterruptedException e) {}
            }
            return con;
        }

        /**
         * Closes all available connections.
         * @return true if the pool is empty and balance
         *         false if the pool has returned some connection to outside
         */
        synchronized boolean release() {
            boolean retValue = true;
            Enumeration allConnections = freeConnections.elements();
            while (allConnections.hasMoreElements()) {
                Connection con = (Connection) allConnections.nextElement();
                try {
                    con.close();
                } catch (SQLException e) {
                    log.error("Cannot close connection in DBConnectionPool.");
                }
            }
            freeConnections.removeAllElements();
            if (checkedOut != 0) {
                retValue = false;
                log.warn("DBConnectionManager: the built-in connection pool is not balanced.");
            }
            checkedOut = 0;
            return retValue;
        }

        /**
         * Creates a new connection, using a userid and password
         * if specified.
         * @todo: check if this method need synchronized
         */
        private Connection newConnection() {
            Connection con = null;
            try {
                if (user == null) {
                    con = DriverManager.getConnection(URL);
                } else {
                    con = DriverManager.getConnection(URL, user, password);
                }
                con.setAutoCommit(true);//thread 804 by trulore
            } catch (SQLException e) {
                log.error("Cannot create a new connection in DBConnectionPool. URL = " + URL, e);
                return null;
            }
            return con;
        }
    }
}

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美优质美女网站| 亚洲欧美一区二区三区极速播放| 久久综合狠狠综合久久综合88 | 欧美揉bbbbb揉bbbbb| www国产成人| 日本不卡视频在线| 欧美优质美女网站| 亚洲国产高清在线观看视频| 男女视频一区二区| 欧美日韩精品综合在线| 亚洲品质自拍视频| 成人黄色a**站在线观看| 精品成人一区二区| 日本怡春院一区二区| 欧美在线啊v一区| 亚洲免费观看高清| 91亚洲精华国产精华精华液| 久久久久久久精| 麻豆国产一区二区| 欧美一级视频精品观看| 亚洲高清不卡在线观看| 91美女在线观看| 亚洲色图欧美激情| av在线不卡观看免费观看| 国产日产欧美精品一区二区三区| 蜜桃精品在线观看| 日韩欧美资源站| 日韩电影一区二区三区| 欧美精品777| 丝袜美腿高跟呻吟高潮一区| 欧美午夜一区二区三区免费大片| 亚洲欧美一区二区久久| 99久久综合狠狠综合久久| 国产精品无遮挡| 99久久99精品久久久久久| 国产精品美女久久久久高潮| 99国产精品久| 亚洲制服丝袜一区| 欧洲一区二区av| 亚洲v中文字幕| 91精品免费在线| 麻豆精品一二三| 久久综合色之久久综合| 精品一区二区三区的国产在线播放| 欧美一区二区久久久| 激情综合色播激情啊| 国产亚洲欧美色| 91蜜桃视频在线| 日韩高清不卡一区| 国产亚洲精品久| 91老师片黄在线观看| 亚洲一二三四区| 欧美一区二区三区人| 激情文学综合丁香| 国产精品嫩草久久久久| 欧美在线你懂的| 久久av老司机精品网站导航| 国产欧美视频在线观看| 色www精品视频在线观看| 日日噜噜夜夜狠狠视频欧美人| 日韩视频一区二区在线观看| 懂色av一区二区在线播放| 亚洲精品自拍动漫在线| 91精品国产黑色紧身裤美女| 国产不卡在线一区| 亚洲国产成人av| 国产天堂亚洲国产碰碰| 欧美亚洲日本国产| 国内精品写真在线观看| 亚洲综合一二三区| 久久精品水蜜桃av综合天堂| 日本高清不卡aⅴ免费网站| 久久成人免费日本黄色| 亚洲码国产岛国毛片在线| 日韩三级免费观看| 在线中文字幕不卡| 高清在线成人网| 奇米亚洲午夜久久精品| 亚洲男人天堂一区| 久久久久久久久久久电影| 欧美另类一区二区三区| 99久久婷婷国产综合精品 | 亚洲第一福利一区| 国产欧美精品日韩区二区麻豆天美| 欧美在线观看视频在线| 成人激情黄色小说| 久久99日本精品| 午夜精品一区二区三区电影天堂 | 国产精品欧美久久久久无广告 | 久久丁香综合五月国产三级网站| 一区二区三区在线观看视频| 国产性色一区二区| 日韩一区二区在线看| 欧美人与z0zoxxxx视频| 欧美在线短视频| 97久久久精品综合88久久| 国产69精品久久99不卡| 国模冰冰炮一区二区| 麻豆成人久久精品二区三区小说| 一区二区三区.www| 亚洲欧美日本韩国| ㊣最新国产の精品bt伙计久久| 久久久亚洲精华液精华液精华液 | 精品视频资源站| 91在线精品一区二区三区| 成人美女视频在线观看18| 国产大陆精品国产| 国产精品一色哟哟哟| 国产精华液一区二区三区| 国产麻豆精品久久一二三| 精品一区二区免费看| 毛片一区二区三区| 美女免费视频一区二区| 久久国产精品色| 美女mm1313爽爽久久久蜜臀| 久久精品国产亚洲高清剧情介绍 | 欧美在线不卡视频| 欧美日韩精品欧美日韩精品一| 91在线视频免费91| 91在线丨porny丨国产| 色噜噜狠狠色综合欧洲selulu| 91亚洲精品乱码久久久久久蜜桃| 91麻豆123| 7799精品视频| 2023国产精品自拍| 欧美激情综合在线| ㊣最新国产の精品bt伙计久久| 亚洲猫色日本管| 日韩国产欧美一区二区三区| 日本va欧美va欧美va精品| 久久成人免费网| zzijzzij亚洲日本少妇熟睡| 一本一本久久a久久精品综合麻豆| 色综合久久88色综合天天| 欧美日本不卡视频| 久久久影院官网| 一区二区三区四区亚洲| 日日夜夜免费精品| 国产乱子轮精品视频| 99免费精品视频| 91精品国产一区二区三区| 久久久精品黄色| 一区二区三区四区乱视频| 日av在线不卡| 成人毛片视频在线观看| 制服视频三区第一页精品| 一区二区三区四区五区视频在线观看| 亚洲va韩国va欧美va精品| 激情综合五月婷婷| 色天使色偷偷av一区二区| 日韩精品一区二区三区蜜臀 | 精品999在线播放| 亚洲精品午夜久久久| 麻豆精品一区二区三区| 91麻豆免费看片| 久久久久国产精品麻豆| 一区二区高清在线| 国产一区欧美一区| 色悠悠久久综合| 久久人人超碰精品| 天堂午夜影视日韩欧美一区二区| 豆国产96在线|亚洲| 欧美日韩一级大片网址| 亚洲国产精品二十页| 日韩不卡一二三区| 色嗨嗨av一区二区三区| 国产午夜亚洲精品羞羞网站| 亚洲成人www| 97久久精品人人澡人人爽| 欧美tickling网站挠脚心| 亚洲精品一二三四区| 国产成人午夜视频| 7777精品伊人久久久大香线蕉| 国产精品毛片无遮挡高清| 久久激情五月激情| 在线成人小视频| 亚洲国产精品久久久男人的天堂 | 欧美国产禁国产网站cc| 欧美午夜电影一区| 中文字幕一区二区三| 国产在线一区二区综合免费视频| 欧美午夜精品免费| 一区二区三区在线免费播放| 成人午夜av在线| 欧美国产乱子伦 | 国产人成亚洲第一网站在线播放| 三级久久三级久久久| 欧美色综合网站| 亚洲欧美另类图片小说| 国产成人亚洲精品青草天美| 精品国产乱码久久久久久夜甘婷婷| 天天色综合成人网| 4438x成人网最大色成网站| 亚洲午夜电影网| 欧美色手机在线观看| 一区二区成人在线| 色av成人天堂桃色av| 亚洲午夜国产一区99re久久| 欧美色区777第一页| 日韩经典中文字幕一区|