-----------------------MysqlConnectionPool 类-----------------------------------
package _20210528;
import java.sql.Connection;
import java.util.Collection;
import java.util.LinkedList;
/**
* @Author Xulihua
* @Date2021/5/28
* @Description
*/
public class MysqlConnectionPool {
private Integer initNum=5; // 初始化connection的数量
private Integer increase=5; // 每次获取Connection对象的数量 5
private Integer min=3; //允许连接池拥有Connection的最小数量
private Integer max=20; //允许连接池拥有Connection的最大数量
private Byte b=1; //同步锁
private boolean xiaogugo=false; //false xiaogu在家 true出去取Connection对象去
//用于存放Connection对象, LinkerList
private LinkedList<Connection> pool=new LinkedList<>();
//pool 实例
private static MysqlConnectionPool poolInstance=null;
private void produceConnection(Integer num){
for (int i = 0; i < num; i++) {
if (!checkPoolMaxNum()){ //不满才添加
xiaogugo=false;
pool.add(JDBCUtils.getConnection());
}
}
}
private boolean checkPoolMaxNum(){
if(pool.size()>max){
return true;
}else {
return false;
}
}
private MysqlConnectionPool(){
// for (int i = 0; i < initNum; i++) {
// pool.add(JDBCUtils.getConnection());
// }
produceConnection(initNum);
}
//查询连接池Connection对象的数量
public Integer count(){
return pool.size();
}
//取连接池中的对象
public Connection getConnnection() {
//拿到pool里的第一个
// return pool.getFirst();
// return pool.get(0);
synchronized (b) {
while (pool.size()==0 ){
System.out.println("warning,连接池没有连接......");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (pool.size() <= min && !xiaogugo ) { // 3
new Thread(new Runnable() {
@Override
public void run() {
produceConnection(increase);
}
}).start();
}
return pool.pop();
}
}
public void close(Connection conn){
synchronized (b) {
if (conn == null) {
return;
} else {
if (checkPoolMaxNum()) {
JDBCUtils.close(conn);
} else {
pool.add(conn);
}
}
}
}
//构造方法私有化
// ------------------懒汉模式 第一次用的时候才创建对象-------------------------
public static MysqlConnectionPool getPoorInstance(){
if (poolInstance==null){
poolInstance = new MysqlConnectionPool();
}
return poolInstance;
}
//----------------------饿汉模式 先创建一个对象----------------------------------------------
// static MysqlConnectionPool poolInstance2= new MysqlConnectionPool();
// static MysqlConnectionPool getPoorInstance(){
// return poolInstance2;
// }
}
------------------------------测试类-------------------------------
package kb11;
import static org.junit.Assert.assertTrue;
import _20210528.MysqlConnectionPool;
import org.junit.Test;
import java.sql.Connection;
/**
* Unit test for simple App.
*/
public class AppTest {
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue(){
MysqlConnectionPool poorInstance = MysqlConnectionPool.getPoorInstance();
System.out.println("连接池数量:"+poorInstance.count());
for (int i = 0; i < 15; i++) {
Connection connnection = poorInstance.getConnnection();
System.out.println(connnection);
// if(i%2==0){
// poorInstance.close(connnection);
// }
}
System.out.println("循环过后连接池数量:"+poorInstance.count());
}
}
Java 数据库连接池
最新推荐文章于 2024-01-25 08:30:00 发布