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

📄 cmspool.java

📁 java 编写的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/dbpool/CmsPool.java,v $
* Date   : $Date: 2002/04/09 14:39:59 $
* Version: $Revision: 1.14 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.dbpool;

import java.sql.*;
import java.util.*;
import com.opencms.core.*;
import source.org.apache.java.util.*;

/**
 * This class is used to create an connection-pool for opencms.
 *
 * @author a.schouten
 */
public class CmsPool extends Thread {

    /**
     * The parameters for this pool.
     */
    private String m_driver;
    private String m_url;
    private String m_user;
    private String m_password;
    private String m_conTestQuery = null;
    private int m_minConn;
    private int m_maxConn;
    private int m_increaseRate;
    private long m_timeout;
    private long m_maxage;
    private String m_poolname;
    private Driver m_originalDriver;

    /**
     * The current amount of connections in this pool.
     */
    private int m_connectionAmount = 0;

    /**
     * The Stack to store the connections in.
     */
    private Stack m_availableConnections = new Stack();

    /**
     * Creates a new Pool.
     * @param poolname - the name of this pool.
     * @param driver - the classname of the driver.
     * @param url - the url to connect to the database.
     * @param user - the user to access the db.
     * @param password - the password to connect to the db.
     * @param minConn - the minimum amount Connections maintained in the pool.
     * @param maxConn - the maximum amount Connections maintained in the pool.
     * @param increaseRate - the rate to increase the the amount of
     * connections in the pool.
     * @param timeout - the timout after a unused connection has to be closed.
     * @exception SQLException - if a SQL-Error occurs.
     */
    public CmsPool(String poolname, String driver, String url, String user,
                String password, int minConn, int maxConn, int increasRate, int timeout, int maxage)
        throws SQLException {
        super(poolname);
        // store the parameters
        m_poolname = poolname;
        m_driver = driver;
        m_url = url;
        m_user = user;
        if(m_user == null) m_user = "";
        m_password = password;
        if(m_password == null) m_password = "";
        m_minConn = minConn;
        m_maxConn = maxConn;
        m_increaseRate = increasRate;
        m_timeout = timeout  * 60 * 1000;
        m_maxage = maxage  * 60 * 1000;

        // register the driver to the driver-manager
        try {
            Class.forName(driver);
        } catch(ClassNotFoundException exc) {
            throw new SQLException("Driver not found: " + exc.getMessage());
        }
        m_originalDriver = DriverManager.getDriver(m_url);

        // create the initial amount of connections
        createConnections(m_minConn);

        // set this a deamon-thread
        setDaemon(true);
        // start the connection-guard for this pool
        start();
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": created");
        }
    }

    /**
     * Creates a new Pool.
     * @param poolname - the name of this pool.
     * @param driver - the classname of the driver.
     * @param url - the url to connect to the database.
     * @param user - the user to access the db.
     * @param password - the password to connect to the db.
     * @param minConn - the minimum amount Connections maintained in the pool.
     * @param maxConn - the maximum amount Connections maintained in the pool.
     * @param increaseRate - the rate to increase the the amount of
     * connections in the pool.
     * @param timeout - the timout after a unused connection has to be closed.
     * @param conTestQuery - the test query to test a connection before
     * delivering. If this is set to null, no test will be performed.
     * @exception SQLException - if a SQL-Error occurs.
     */
    public CmsPool(String poolname, String driver, String url, String user,
                String password, int minConn, int maxConn, int increasRate,
                int timeout, int maxage, String conTestQuery)
        throws SQLException {
        this(poolname, driver, url, user, password, minConn, maxConn,
            increasRate, timeout, maxage);
        m_conTestQuery = conTestQuery;
    }
    /**
     * The run-method for the connection-guard
     */
    public void run() {
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
           A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": starting connection-guard");
        }
        // never stop
        for(;;) {
            // sleep, before checking the timeouts of the connections
            try {
                sleep(m_timeout);
            } catch(InterruptedException exc) {
                // ignore this exception
            }
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
               A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": checking for outtimed connections");
            }
            synchronized(m_availableConnections) {
                Enumeration elements = m_availableConnections.elements();
                while(elements.hasMoreElements()) {
                    CmsConnection con = (CmsConnection) elements.nextElement();
                    if((con.getLastUsed() + (m_timeout)) < System.currentTimeMillis()) {
                        // this connection is to old... destroy it
                        m_availableConnections.removeElement(con);
                        m_connectionAmount--;
                        con.closeOriginalConnection();
                        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                           A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": closing one outtimed connection");
                        }
                    }
                }
                // create missing minimum connection-amount
                if(m_connectionAmount < m_minConn) {
                    try {
                        createConnections(m_minConn - m_connectionAmount);
                    } catch(SQLException exc) {
                        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                            A_OpenCms.log(I_CmsLogChannels.C_OPENCMS_POOL, "["+ getClass().getName() +"] " + m_poolname + ": unable to create new connection for broken one");
                        }
                    }
                }
            }
        }
    }


    /**
     * Try to make a database connection to the given database.
     * @return a Connection to the database
     * @exception SQLException if a database-access error occurs.
     */
    public Connection connect() throws SQLException {
        return getConnection();
    }

    /**
     * Gets a connection.
     * @exception SQLException if a database-access error occurs.
     */
    private Connection getConnection()
        throws SQLException {
        Connection con = null;

⌨️ 快捷键说明

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