栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021.12.31连接池简单实现学习记录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021.12.31连接池简单实现学习记录

ConstClass
public class ConstClass {

    public static final String MYDB_CONF_NAME="mydb.properties";
    public static final String MYSQL_DRIVER_STR="mysqldriver";
    public static final String MYSQL_USER_STR="user";
    public static final String MYSQL_PWD_STR="password";
    public static final String MYSQL_URL_STR="url";
    public static final String MYSQL_MAX_STR="max";
    public static final String MYSQL_MIN_STR="min";
    public static final String MYSQL_INCREASE_STR="increase";
    public static final String MYSQL_INIT_STR="initNum";

}
JdbcUtils 
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String pwd;

    static {
        try {
            Properties properties = PropertyUtils.getProperties(ConstClass.MYDB_CONF_NAME);
            driver = properties.getProperty(ConstClass.MYSQL_DRIVER_STR);
            url = properties.getProperty(ConstClass.MYSQL_URL_STR);
            user = properties.getProperty(ConstClass.MYSQL_USER_STR);
            pwd = properties.getProperty(ConstClass.MYSQL_PWD_STR);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    // 释放Connection连接
    public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(pstmt!=null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, PreparedStatement pstmt){
        close(conn, pstmt, null);
    }

    public static void close(Connection conn){
        close(conn,null,null);
    }

    public static void main(String[] args) {
        for(int i=0; i<200;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Connection connection = JdbcUtils.getConnection();
                    System.out.println(connection);
                    JdbcUtils.close(connection);
                }
            }).start();

        }

    }
}
MyConnectionPool 
import java.io.IOException;
import java.sql.Connection;
import java.util.linkedList;
import java.util.Properties;

public class MyConnectionPool {
    private Integer initNum = 100;   // 初始化Connection的数量
    private Integer min = 30;       // 允许连接池中拥有Connection的最小数量
    private Integer max = 150;      // 允许连接池中拥有Connection的最大数量
    private Integer increase = 30;  // 每次与数据库创建Connection的数量

    private Byte b = 1;  // 同步锁 标记
    private Boolean xiaofango = false; // 跑腿去购买Connection的人,是否已经出去购买 true:表示已经去购买 false:表示在家


    // 用于存放Connection对象
    private linkedList pool = new linkedList<>();

    // 用于创建 Connection 的私有方法
    private void produceConnection(Integer num){
        for (int i = 0; i < num; i++) {
            if(checkPoolMaxNum()) {
                //  创建 Connection 对象
                Connection conn = JdbcUtils.getConnection();
                pool.add(conn);
                xiaofango=false;
            }
        }
    }

    private boolean checkPoolMaxNum(){
        if(pool.size()>=max){
            return false;
        }
        return true;
    }


    private MyConnectionPool(){
        try {
            Properties prop = PropertyUtils.getProperties("mydb.properties");
            String property = prop.getProperty(ConstClass.MYSQL_INCREASE_STR);

            increase = prop.getProperty(ConstClass.MYSQL_INCREASE_STR)==null?increase: Integer.parseInt(prop.getProperty(ConstClass.MYSQL_INCREASE_STR)) ;
            max = prop.getProperty(ConstClass.MYSQL_MAX_STR)==null?max: Integer.parseInt(prop.getProperty(ConstClass.MYSQL_MAX_STR));
            min =prop.getProperty(ConstClass.MYSQL_MIN_STR)==null? min: Integer.parseInt(prop.getProperty(ConstClass.MYSQL_MIN_STR));
            initNum = prop.getProperty(ConstClass.MYSQL_INIT_STR)==null? increase:Integer.parseInt(prop.getProperty(ConstClass.MYSQL_INIT_STR));
            produceConnection(initNum);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 提供 Connection 对象
    public Connection getConnection(){
        synchronized (b){
            while (pool.size()==0){
                System.out.println("waring,连接池中没有Connection对象。。。。。。");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(pool.size()<=min && !xiaofango){
                xiaofango=true;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        produceConnection(increase);
                    }
                }).start();
            }
            return pool.pop();
        }
    }


    // 还回 Connection 对象
    public void close(Connection conn){
        if(pool.size()>max){
            JdbcUtils.close(conn);
        }else {
            pool.add(conn);
        }
    }

    private static MyConnectionPool poolInstance = new MyConnectionPool();
    public static MyConnectionPool getPoolInstance(){
        return poolInstance;
    }

    public static void main(String[] args) {
//        MyConnectionPool myConnectionPool = new MyConnectionPool();
        for (int i = 0; i < 10; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
//
                    MyConnectionPool source = MyConnectionPool.getPoolInstance();
                    Connection connection = source.getConnection();
                    System.out.println(Thread.currentThread().getName()+""+ connection);
                    source.close(connection);
                }
            }).start();

        }
    }
}



 MyPoolUtils
import java.sql.Connection;

public class MyPoolUtils {
    static MyConnectionPool source=MyConnectionPool.getPoolInstance();

    public static Connection getConnection(){
        Connection connection=null;
        connection =source.getConnection();
        return connection;
    }
    public static void close(Connection connection){
        source.close(connection);}

    public static void main(String[] args) {
        for(int i=0;i<5;i++){
            Connection connection = MyPoolUtils.getConnection();
            System.out.println(connection);
            MyPoolUtils.close(connection);
        }
    }
}

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

public class PropertyUtils {
    public static Properties getProperties(String proFileName) throws IOException {
        ClassLoader classLoader = PropertyUtils.class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream(proFileName);
        Properties props = new Properties();
        props.load(is);
        return props;
    }

    public static void main(String[] args) throws IOException {
        Properties props = PropertyUtils.getProperties("mydb.properties");
        String driver = props.getProperty("mysqldriver");
        String url = props.getProperty("url");
        String user = props.getProperty("user");
        String pwd = props.getProperty("password");
        System.out.println(driver);
        System.out.println(url);
        System.out.println(user);
        System.out.println(pwd);
    }
}
 TestMyPool
public class TestMyPool {
    public static void main(String[] args) {
//        MyConnectionPool myConnectionPool = new MyConnectionPool();
    }
}

 

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/693814.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号