数据库连接池

  数据库连接池,what we need is a simple connection pool ,hibernate ,ibatis神马的还是很庞大。

  So,I plan to build my own connection poll.

  How does it work ,we can follow the steps following.

(1)建立数据库连接池对象(服务器启动)。
(2)按照事先指定的参数创建初始数量的数据库连接(即:空闲连接数)。
(3)对于一个数据库访问请求,直接从连接池中得到一个连接。如果数据库连接池对象中没有空闲的连接,且连接数没有达到最大(即:最大活跃连接数),创建一个新的数据库连接。
(4)存取数据库。
(5)关闭数据库,释放所有数据库连接(此时的关闭数据库连接,并非真正关闭,而是将其放入空闲队列中。如实际空闲连接数大于初始空闲连接数则释放连接)。
(6)释放数据库连接池对象(服务器停止、维护期间,释放数据库连接池对象,并释放所有连接)。

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;

public class ConnectionManager
{
    private int inUse = 0;

    private String dbid =  "jdbc:mysql://localhost:3306/teasystem";
    
    private String drivername = "";

    private String username = "root";

    private String password = "123";

    private int maxconn = 20;

    private static ConnectionManager cm = new ConnectionManager();

    private ConnectionManager()
    {
        pool = new Vector<Connection>(maxNum);
    }

    // 连接池最大数量
    private int maxNum = 20;

    // 保存连接对象
    private Vector<Connection> pool;

    public synchronized Connection getConnection()
    {
        Connection con = null;
        if (pool.size() > 0)
        {
            con = (Connection) pool.elementAt(0);
            pool.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<Connection> allConnections = pool.elements();
        while (allConnections.hasMoreElements())
        {
            Connection con = (Connection) allConnections.nextElement();
            try
            {
                con.close();
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static ConnectionManager getInstance()
    {
        if (null == cm)
        {
            return new ConnectionManager();
        }

        return cm;
    }

    public synchronized void reConnection(Connection conn)
    {
        Connection con = conn;
        pool.addElement(con);
        inUse--;

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值