配置文件
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/kjc?&useSSL=false&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
#空闲最小连接数
minConnections = 1
#初始连接数
initialSize = 5
#空闲最大连接数
maxConnections = 10
#重复获得的链接频率秒
connTimeOt = 1000
#最大允许连接数 和数据库对应--默认情况下数据库配置是100
maxActiveConnections = 100
#最大的有效连接数 和数据库对应
maxActive = 100
#最大等待数
maxWait = 200
连接池工具类
package com.cjk.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
public class DBPool {
static String driver = null;
static String url = null;
static String name = null;
static String pwd = null;
int cont = 0;
static int minConnections = 0;//#空闲最小连接数
static int initialSize = 0;// #初始连接数
static int maxConnections = 0;// #空闲最大连接数
static long connTimeOt = 0;// #重复获得的链接频率秒
static int maxActiveConnections = 0;// #最大允许连接数 和数据库对应
private List<Connection> freeConnection = new Vector<Connection>();//空闲
private List<Connection> activeConnection = new Vector<Connection>();//活动
static
{
Properties ps = new Properties();
InputStream is = DBConnection.class.getClassLoader().getResourceAsStream("db.properties");
try {
if (is == null) throw new Exception("sql连接文件空指针");
ps.load(is);
driver = ps.getProperty("jdbc.driver");
url = ps.getProperty("jdbc.url");
name = ps.getProperty("jdbc.username");
pwd = ps.getProperty("jdbc.password");
minConnections = Integer.parseInt(ps.getProperty("minConnections"));
initialSize = Integer.parseInt(ps.getProperty("initialSize"));
maxConnections = Integer.parseInt(ps.getProperty("maxConnections"));
connTimeOt = Integer.parseInt(ps.getProperty("connTimeOt"));
maxActiveConnections = Integer.parseInt(ps.getProperty("maxActiveConnections"));
} catch (Exception e) {
e.printStackTrace();
}
}
//空闲线程初始化
public DBPool()
{
for (int i = 0; i < initialSize; i++) {
Connection connection = NewConnection();
if (connection!=null)
{
freeConnection.add(connection);//添加到空闲线程
}
}
}
private synchronized Connection NewConnection()
{
Connection connection = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, name, pwd);
} catch (Exception e) {
e.printStackTrace();
}
cont++;
return connection;
}
//判断是否可用
private boolean isAvalanche(Connection connection)
{
try {
if (connection==null || connection.isClosed())
{
return false;
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return true;
}
//获取链接
public Connection getConnection()
{ //判断链接数是否小于 最大允许的连接数
Connection connection = null;
if (cont < maxActiveConnections)
{ //小于最大连接数
if (freeConnection.size() > 0) {
connection = freeConnection.remove(0);//拿到再删除。等同于freeConnection.get(0);freeConnection.remove(0)
}else
{
connection = NewConnection();//重新创建
}
//判断是否可用
boolean b = isAvalanche(connection);
if (b)
{
activeConnection.add(connection);
}else {//如果链接不可用,尝试重新调用一下
cont--;
connection = getConnection();
}
}else
{ //大于最大链接数
try {
wait(connTimeOt);
connection = getConnection();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return connection;
}
//回收机制
public synchronized void releaseConnection(Connection connection)
{
if (isAvalanche(connection))
{
if (freeConnection.size() < maxConnections)
{
freeConnection.add(connection);//空闲线程回收链接
}
else {
//空闲线程已经满了。关闭
try {
connection.close();
activeConnection.remove(connection);
cont--;
notifyAll();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
建议使用静态单例类,确保连接池只有一个实例
package com.cjk.util;
public class DBBean {
private static DBPool dbPool;
private DBBean()
{
}
public static synchronized DBPool getDbPool()
{
if (dbPool == null)
{
dbPool = new DBPool();
return dbPool;
}
return dbPool;
}
}