瞎j8封装第二版之数据库连接池

Posted yeyeck

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了瞎j8封装第二版之数据库连接池相关的知识,希望对你有一定的参考价值。

写得很蛋疼,本来想支持多线程的,奈何对多线程和连接池理解着实太菜;

所以,起码是能拿到连接了。。。

但是还是不太懂这个连接池

 

我也是半抄别人的,以后再搞一搞这个吧。

先是配置文件 理想是很丰满的,奈何现实。。。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true
jdbc.username=root
jdbc.password=yck940522


#最小连接数
jdbc.minSize=3    
#最大连接数
jdbc.maxSize=50
#初始化连接数
jdbc.initSize=5
#重试次数
jdbc.tryTimes=2
#延迟时间
jdbc.delay=1000
jdbc.maxActiveSize=100
jdbc.timeOut=1200000

jdbc.check = true
jdbc.checkTime = 30000

配了那么多参数,很多都没用上。。唉,还是太菜;

package jdbc;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class DataBase {
    private static String username;
    private static String password;
    private static String url;
    private static String driver;

    private static Integer minSize;
    private static Integer maxSize;
    private static Integer initSize;
    private static Integer maxActiveSize;
    private static Integer tryTimes;
    private static Long delay;
    private static Long timeOut;
    private static Boolean checked;
    private static Long checkTime;

    private static DataBase instance;

    private DataBase(){
        InputStream in = DataBase.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties p = new Properties();
        try {
            p.load(in);
            username = p.getProperty("jdbc.username");
            password = p.getProperty("jdbc.password");
            url = p.getProperty("jdbc.url");
            driver = p.getProperty("jdbc.driver");
            minSize = Integer.valueOf(p.getProperty("jdbc.minSize","3"));
            maxSize = Integer.valueOf(p.getProperty("jdbc.maxSize","20"));
            initSize = Integer.valueOf(p.getProperty("jdbc.initSize","5"));
            maxActiveSize = Integer.valueOf(p.getProperty("jdbc.maxActiveSize","100"));
            tryTimes =Integer.valueOf(p.getProperty("jdbc.tryTimes","2"));
            delay = Long.valueOf(p.getProperty("jdbc.delay","1000"));
            timeOut = Long.valueOf(p.getProperty("jdbc.timeOut","1200000"));
            checked = Boolean.valueOf(p.getProperty("jdbc.check","false"));
            checkTime = Long.valueOf(p.getProperty("jdbc.checkTime","30000"));

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static DataBase getInstance(){
        if(instance == null){
            synchronized (DataBase.class){
                if(instance == null){
                    instance = new DataBase();
                }
            }
        }
        return instance;
    }

    public  String getUsername() {
        return username;
    }

    public  String getPassword() {
        return password;
    }

    public  String getUrl() {
        return url;
    }

    public  String getDriver() {
        return driver;
    }

    public  Integer getMinSize() {
        return minSize;
    }

    public  Integer getMaxSize() {
        return maxSize;
    }

    public  Integer getInitSize() {
        return initSize;
    }

    public  Integer getMaxActiveSize() {
        return maxActiveSize;
    }

    public  Integer getTryTimes() {
        return tryTimes;
    }

    public  Long getDelay() {
        return delay;
    }

    public  Long getTimeOut() {
        return timeOut;
    }

    public Boolean getChecked() {
        return checked;
    }

    public Long getCheckTime() {
        return checkTime;
    }
}

对单例也不太懂,瞎写,有经验的大佬指正一下啊

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

public class ConnectionPool{

    private static final Long lazyTime = 30000L;

    private DataBase dataBase;
    private AtomicInteger totalSize = new AtomicInteger(0);
    private List<Connection> freeConnections = new Vector<Connection>();
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();

    private static ConnectionPool  instance;

    private ConnectionPool(){
        this.dataBase = DataBase.getInstance();
        init();
    }

    public static ConnectionPool getInstance(){
        if(instance == null){
            synchronized (ConnectionPool.class){
                if(instance == null){
                    instance = new ConnectionPool();
                }
            }
        }
        return instance;
    }

    private void init(){
        try {
            Class.forName(dataBase.getDriver());
            for(int i=0;i<dataBase.getInitSize();i++){
                Connection connection = createConnection();
                freeConnections.add(connection);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    private synchronized Connection createConnection(){
        try {
            Class.forName(dataBase.getDriver());
            Connection conn= DriverManager.getConnection(dataBase.getUrl(),dataBase.getUsername(),dataBase.getPassword());
            totalSize.incrementAndGet();
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    private synchronized Connection getConnection() {
        Connection conn= null;
        try {
            if(totalSize.get() < dataBase.getMaxSize()){
                if(freeConnections.size()>0){
                    conn = freeConnections.get(0);
                    if(conn != null){
                        threadLocal.set(conn);
                    }
                    freeConnections.remove(0);
                }else {
                    conn = createConnection();
                }
            }else {
                wait(dataBase.getDelay());
                conn = getConnection();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return conn;
    }

    private boolean isValid(Connection conn){
        try {
            if(conn == null || conn.isClosed()){
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    public synchronized Connection getCurrentConnection() {
        Connection conn = threadLocal.get();
        if(!isValid(conn)){
            return getConnection();
        }
        return conn;
    }

    public void checkPool() {
        if(dataBase.getChecked()){
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    System.out.println("空线池连接数:"+freeConnections.size());
                    System.out.println("总的连接数:"+totalSize.get());
                }
            }, lazyTime, dataBase.getCheckTime());
        }
    }

}

这个连接池我就不做什么说明了。。。自己只能理解最简单的。。。简单的说就是先初始化一部分连接放在一个list里,要用的时候就去取,如果没有超过上限也不用close了。。但是我一直没搞明白怎么去判断它空闲了多长时间然后close掉。。。所以很多也没实现。

 

大王让我写代码 

2017-12-30

以上是关于瞎j8封装第二版之数据库连接池的主要内容,如果未能解决你的问题,请参考以下文章

瞎j8封装第二版之用xml文件来代理dao接口

Huawei_Netconf_Ncclient

python核心编程第二版

自己瞎捉摸学习:十三种设计模式之模板方式设计

数据库连接池的选择 | 实现

Windows 系统安装的两个阶段