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

📄 dbconnectionmanager$dbconnectionpool.java

📁 动态网站管理发布系统
💻 JAVA
字号:
package com.ntsky.pool;

import com.ntsky.common.Debug;
import java.sql.*;
import java.util.*;
import java.util.Date;


class DBConnectionPool
{

    private int checkedOut;
    private Vector freeConnections;
    private int maxConn;
    private String name;
    private String user;
    private String password;
    private String URL;

    public synchronized void freeConnection(Connection Conn)
    {
        freeConnections.addElement(Conn);
        checkedOut--;
        notifyAll();
    }

    public synchronized Connection getConnection()
    {
        Connection con = null;
        if(freeConnections.size() > 0)
        {
            con = (Connection)freeConnections.firstElement();
            freeConnections.removeElementAt(0);
            try
            {
                if(con.isClosed())
                {
                    Debug.writeLog("从连接池" + name + "删除一个无效连接");
                    con = getConnection();
                }
            }
            catch(SQLException e)
            {
                Debug.writeLog("从连接池" + name + "删除一个无效连接");
                con = getConnection();
            }
        } else
        if(maxConn == 0 || checkedOut < maxConn)
            con = newConnection();
        if(con != null)
            checkedOut++;
        if(con == null)
            Debug.writeLog("DBConnectionPool getConnection(), The Returned Con is null");
        return con;
    }

    public synchronized Connection getConnection(long timeout)
    {
        long startTime = (new Date()).getTime();
        Connection con;
        while((con = getConnection()) == null) 
        {
            try
            {
                wait(timeout);
            }
            catch(InterruptedException e) { }
            if((new Date()).getTime() - startTime >= timeout)
                return null;
        }
        return con;
    }

    private Connection newConnection()
    {
        Connection con = null;
        try
        {
            if(user == null)
                con = DriverManager.getConnection(URL);
            else
                con = DriverManager.getConnection(URL, user, password);
                Debug.writeLog("连接池" + name + "创建一个新的连接");
        }
        catch(SQLException e)
        {
            Debug.writeLog("无法创建下列URL的连接: " + URL);
            e.printStackTrace(System.out);
            return null;
        }
        if(con == null)Debug.writeLog("DBConnectionPool newConnection(), The Returned Con is null");
        return con;
    }

    public synchronized void release()
    {
        for(Enumeration allConnections = freeConnections.elements(); allConnections.hasMoreElements();)
        {
            Connection con = (Connection)allConnections.nextElement();
            try
            {
                con.close();
                Debug.writeLog("关闭连接池" + name + "中的一个连接");
            }
            catch(SQLException e)
            {
                Debug.writeLog("无法关闭连接池" + name + "中的连接");
                e.printStackTrace(System.out);
            }
        }

        freeConnections.removeAllElements();
    }

    public DBConnectionPool(String name, String url, String user, String password, int maxConn)
    {
        freeConnections = new Vector();
        this.name = name;
        URL = url;
        this.user = user;
        this.password = password;
        this.maxConn = maxConn;
        Debug.writeLog("poolname" + this.name);
        Debug.writeLog("URL: " + URL);
        Debug.writeLog("user: " + this.user);
        Debug.writeLog("password: " + this.password);
        Debug.writeLog("maxConn: " + this.maxConn);
    }
}

⌨️ 快捷键说明

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