∑ni=0i2=(n2+n)(2n+1)6
∑i=0ni2=(n2+n)(2n+1)6
111xyzx2y2z2
package TestJDBC.DBConnPool;
import java.util.ArrayList;
import java.util.List;
import java.sql.*;
import TestJDBC.jdbcUtil.jdbcUtil;
public class DBConnPool {
//连接池(connection pool)
//就是将connection对象放入List中,反复使用
//连接池的初始化
//事先放入多个连接对象
//从连接池中获取连接对象
//如果池中有可用连接,则将池中最后一个返回,同时,将该连接从池中remove,表示正在使用
//如果池中无可用连接,则创建一个新的
//关闭连接
//不是真正的关闭,而是将使用完的放回去
//连接池对象
private static List<Connection> pool;
//最小连接数
private static final int POOL_MIN_SIZE = 10;
//最大连接数
private static final int POOL_MAX_SIZE = 100;
//声明一个临时变量来计算连接对象的数量
private int currentsize = 0;
public DBConnPool() {
initPool();
}
//初始化连接池,让池中的连接数达到最小值
public void initPool(){
if(pool == null){
pool = new ArrayList<Connection>();
}
while(pool.size()<POOL_MIN_SIZE){
pool.add(jdbcUtil.createConnection());
System.out.println("初始化连接池,池中连接数:"+pool.size());
currentsize++;
}
}
//从连接池中获取连接对象,取池中最后一个
public synchronized Connection getConnecton(){
int last_index;
//池中有可用连接
if(pool.size()>0){
last_index = pool.size()-1;
Connection conn = pool.get(last_index);
pool.remove(last_index);
return conn;
}
//连接池被拿空,且连接数没有达到上限,创建新的连接
else if(pool.size() == 0 && currentsize<POOL_MAX_SIZE){
System.out.println("没有资源了,创建一个新的");
pool.add(jdbcUtil.createConnection());
currentsize++;
System.out.println(currentsize);
Connection conn = pool.get(0);
pool.remove(0);
return conn;
}
throw new RuntimeException("连接数达到上限,请等待");
}
public synchronized void close(Connection conn){
pool.add(conn);
}
}
- 列表内容,
Title: Here is a title
A->B: Normal line
B–>C: Dashed line
C->>D: Open arrow
D–>>A: Dashed open arrow