Javaweb--手写数据库连接池

 配置文件

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/kjc?&useSSL=false&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

#空闲最小连接数
minConnections = 1
#初始连接数
initialSize = 5
#空闲最大连接数
maxConnections = 10
#重复获得的链接频率秒
connTimeOt = 1000
#最大允许连接数 和数据库对应--默认情况下数据库配置是100
maxActiveConnections = 100
#最大的有效连接数 和数据库对应
maxActive = 100
#最大等待数
maxWait = 200

连接池工具类

package com.cjk.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

public class DBPool {

    static  String driver = null;
    static  String url = null;
    static  String name = null;
    static  String pwd = null;
    int cont = 0;
    static  int minConnections = 0;//#空闲最小连接数
    static  int initialSize = 0;//  #初始连接数
    static  int maxConnections = 0;//  #空闲最大连接数
    static  long connTimeOt = 0;//  #重复获得的链接频率秒
    static  int  maxActiveConnections = 0;//  #最大允许连接数 和数据库对应
    private List<Connection> freeConnection =  new Vector<Connection>();//空闲
    private List<Connection> activeConnection =  new Vector<Connection>();//活动
    static
    {
        Properties ps = new Properties();
        InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            if (is == null) throw  new Exception("sql连接文件空指针");
            ps.load(is);
            driver = ps.getProperty("jdbc.driver");
            url = ps.getProperty("jdbc.url");
            name = ps.getProperty("jdbc.username");
            pwd = ps.getProperty("jdbc.password");
            minConnections = Integer.parseInt(ps.getProperty("minConnections"));
            initialSize = Integer.parseInt(ps.getProperty("initialSize"));
            maxConnections = Integer.parseInt(ps.getProperty("maxConnections"));
            connTimeOt = Integer.parseInt(ps.getProperty("connTimeOt"));
            maxActiveConnections = Integer.parseInt(ps.getProperty("maxActiveConnections"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //空闲线程初始化
    public DBPool()
    {
        for (int i = 0; i < initialSize; i++) {
            Connection connection = NewConnection();
            if (connection!=null)
            {
                freeConnection.add(connection);//添加到空闲线程
            }
        }

    }

    private synchronized Connection NewConnection()
    {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, name, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cont++;
        return connection;

    }
    //判断是否可用
    private boolean isAvalanche(Connection connection)
    {

        try {
            if (connection==null || connection.isClosed())
            {
                return false;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return true;
    }

    //获取链接
    public Connection getConnection()
    {   //判断链接数是否小于 最大允许的连接数
        Connection connection = null;
        if (cont < maxActiveConnections)
        {   //小于最大连接数
            if (freeConnection.size() > 0) {
                connection = freeConnection.remove(0);//拿到再删除。等同于freeConnection.get(0);freeConnection.remove(0)
            }else
            {
                connection = NewConnection();//重新创建
            }
            //判断是否可用
            boolean b = isAvalanche(connection);
            if (b)
            {
                activeConnection.add(connection);
            }else {//如果链接不可用,尝试重新调用一下
                cont--;
                connection = getConnection();
            }

        }else
        {   //大于最大链接数
            try {
                wait(connTimeOt);
                connection = getConnection();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        return connection;
    }


    //回收机制
    public synchronized void releaseConnection(Connection connection)
    {
        if (isAvalanche(connection))
        {
            if (freeConnection.size() < maxConnections)
            {
                freeConnection.add(connection);//空闲线程回收链接
            }
            else {
                //空闲线程已经满了。关闭
                try {
                    connection.close();
                    activeConnection.remove(connection);
                    cont--;
                    notifyAll();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

        }


    }

}

建议使用静态单例类,确保连接池只有一个实例 

package com.cjk.util;

public class DBBean {

    private static DBPool dbPool;

    private DBBean()
    {

    }
    public static synchronized DBPool getDbPool()
    {
        if (dbPool == null)
        {
            dbPool = new DBPool();
            return dbPool;
        }
        return dbPool;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一剑封喉の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值