JAVA MySQL连接池

  1. ConnectionPool() 构造方法约定了这个连接池一共有多少连接

  2. 在init() 初始化方法中,创建了size条连接。 注意,这里不能使用try-with-resource这种自动关闭连接的方式,因为连接恰恰需要保持不关闭状态,供后续循环使用

  3. getConnection, 判断是否为空,如果是空的就wait等待,否则就借用一条连接出去

  4. returnConnection, 在使用完毕后,归还这个连接到连接池,并且在归还完毕后,调用notifyAll,通知那些等待的线程,有新的连接可以借用了。

package mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConnectionPool {


    List<Connection> cs = new ArrayList<Connection>();

    int size;

    public ConnectionPool(int size) {
        this.size = size;
        init();
    }

    public void init() {
        //这里恰恰不能使用try-with-resource的方式,因为这些连接都需要是"活"的,不要被自动关闭了
        try {
            Class.forName("com.mysql.jdbc.Driver");
            for (int i = 0; i < size; i++) {
                Connection c = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1/user?characterEncoding=UTF-8",
                "root","admin");
                System.out.println("完成第"+ ( i + 1 ) +"个连接");
                cs.add(c);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	//synchronized 线程安全性
    public synchronized Connection getConnection() {
        while (cs.isEmpty()) {
            try {
            //如果连接池中没有连接可用则等待
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //移除第一个连接池
        Connection c = cs.remove(0);
        return c;
    }

    public synchronized void returnConnection(Connection c) {
    //使用完给回连接池中
        cs.add(c);
    //唤醒wait告诉它有新的连接池可用
        this.notifyAll();
    }

}


下面为main部分
首先初始化一个有3条连接的数据库连接池
然后创建100个线程,每个线程都会从连接池中借用连接,并且在借用之后,归还连接。 拿到连接之后,执行一个耗时3秒的SQL语句

package mysql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class start {
    public static void main(String[] args) {
        ConnectionPool cp = new ConnectionPool(3);
        for(int i = 0; i < 100; i++ ){
            new WorkingThread("t"+i,cp).start();

        }
    }
    static class WorkingThread extends Thread {
        private ConnectionPool cp;

        public WorkingThread(String name, ConnectionPool cp) {
            super(name);
            this.cp = cp;
        }

        public void run() {
            Connection c = cp.getConnection();

            System.out.println(this.getName() + ":\t 获取了一根连接,并开始工作");

            try (Statement st = c.createStatement()) {

                //占用连接耗时3s
                Thread.sleep(3000);
                //hero为数据库表
                st.execute("select * from hero");

            } catch (SQLException | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cp.returnConnection(c);
        }
    }
}

效果图如下
在这里插入图片描述

出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为'Protocol'的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name 'Protocol' from 'typing'错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.youkuaiyun.com/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值