數據庫連接池的優點和原理

數據庫連接是非常寶貴的系統資源,連接一次數據庫,底層程序需要經過很多步驟,花費比較多的時間,如果每次要操作數據庫的時候纔開始建立數據庫連接,用完之後再關閉連接,勢必造成程序的效率問題。
連接池的基本原理是,先初始化一定的數據庫連接對象,並且把這些連接保存在連接池中。當程序需要訪問數據庫的時候,從連接池中取出一個連接,數據庫操作結束後,再把這個用完的連接重新放回連接池。
當然以上我說的是隻是一個最簡單的工作原理,連接池本身是比較複雜的,裏面涉及到併發的控制,連接的提取,回收算法,連接不夠時的相應等等。
package test;
import java.sql.*;
import java.util.*;

public class DBConnpool
{
    private int inUse = 0;
    private Vector<Connection> connections = new Vector<Connection>();
    private String poolname = "dbconnpool";
    private String dbid = "jdbc:mysql://localhost:3306/teasystem";
    private String drivername = "com.mysql.jdbc.Driver";
    private String username = "root";
    private String password = "123";
    private int maxconn = 5000;
    
    public DBConnpool(){}
    public void setdbid(String dbid)
    {
        this.dbid = dbid;
    }
    public void setusername(String username)
    {
        this.username = username;
    }
    public void setpassword(String password)
    {
        this.password = password;
    }
    public void setmaxconn(int maxconn)
    {
        this.maxconn = maxconn;
    }
    public String getdbid()
    {
        return dbid;
    }
    public String getusername()
    {
        return username;
    }
    public String getpassword()
    {
       return password;
    }
    public int getmaxconn()
    {
        return maxconn;
    }
    //將連接返還給連接池
    public synchronized void reConnection(Connection conn)
    {
     Connection con = conn;
        connections.addElement(con);
        inUse--;
    }
    //從連接池獲取一個連接
    public synchronized Connection getConnection()
    {
        Connection con = null;
        if(connections.size()>0)
        {
            con = (Connection)connections.elementAt(0);
            connections.removeElementAt(0);
            try{
                if(con.isClosed())
                {
                    con = getConnection();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }else if(maxconn == 0||inUse<maxconn)
        {
            con = newConnection();
        }
        if(con != null)
        {
            inUse++;
        }
        return con;
    }
    private Connection newConnection()
    {
        Connection con = null;
        try{
            Class.forName(drivername);
            con = DriverManager.getConnection(dbid,username,password);
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
        return con;
    }
    public synchronized void closeConn()
    {
        Enumeration allConnections = connections.elements();
        while(allConnections.hasMoreElements())
        {
            Connection con = (Connection)allConnections.nextElement();
            try{
                con.close();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}

使用連接池,把暫時不使用的鏈接放入連接池,到需要使用的時候,從連接池中取出鏈接使用

發佈了30 篇原創文章 · 獲贊 3 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章