-
ConnectionPool() 构造方法约定了这个连接池一共有多少连接
-
在init() 初始化方法中,创建了size条连接。 注意,这里不能使用try-with-resource这种自动关闭连接的方式,因为连接恰恰需要保持不关闭状态,供后续循环使用
-
getConnection, 判断是否为空,如果是空的就wait等待,否则就借用一条连接出去
-
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);
}
}
}
效果图如下