首页 技术 正文
技术 2022年11月14日
0 收藏 410 点赞 4,457 浏览 3009 个字
package cn.hc.connectionPool;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;/**
* 简单的MySql数据库连接池
* @author hc
* @version 1.0
*/
public class ConnectionPool {
private LinkedList<Connection> connections=new LinkedList<Connection>();
//默认的数据库连接池大小为5
private static int initSize=5;
private static int maxSize=5;
private int acturalSize=0;//数据库连接池的实际大小
private static Properties props = null;
static{
try {
InputStream in = ConnectionPool.class.getClassLoader()
.getResourceAsStream("dbconfig.properties");
props = new Properties();
props.load(in);
} catch(IOException e) {
throw new RuntimeException(e);
} try {
String size=props.getProperty("initSize");
String size2=props.getProperty("maxSize");
if(size!=null){
initSize=Integer.parseInt(size);
}
if(size2!=null){
maxSize=Integer.parseInt(size2);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//获取连接的静态内部类
static class GetCon{
static{
try {
Class.forName(props.getProperty("driverClassName"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static Connection getConnection() throws SQLException{
return DriverManager.getConnection(props.getProperty("url"),
props.getProperty("username"),
props.getProperty("password"));
}
}
public ConnectionPool(){
//初始化数据库连接池
for (int i = 0; i<initSize; i++) {
try {
connections.addLast(GetCon.getConnection());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
acturalSize++; }
}
/**
* 该方法用来释放连接,将connection对象放回到数据库连接池,实现对数据库连接池大大小的增减
* @param connection 要放回数据库连接池的连接
*/
public void releseConnection(Connection connection){
if(connection!=null){
synchronized (connections) {
if(connections.size()>initSize){
try {
connection.close();
acturalSize--;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }else{
connections.addLast(connection);
} connections.notifyAll();//唤醒所有等待获取连接的对象
}
}
}
/**
* 该方法用来从数据库连接池获取连接
* @param mills 获取连接的超时时间 单位毫秒,当设置的值为0时候,即不要求等待时间
* @return connection对象
* @throws SQLException
* @throws InterruptedException
*/
public Connection getConnection(long mills) throws SQLException, InterruptedException{
synchronized (connections) {
if(mills<=0){
while (connections.isEmpty()) {
if(acturalSize<maxSize){
Connection con= DriverManager.getConnection(props.getProperty("url"),
props.getProperty("username"),
props.getProperty("password"));
acturalSize++;
return con;
}else{
connections.wait();
}
}
return connections.removeFirst();
}else{
if(acturalSize<maxSize){
Connection con= DriverManager.getConnection(props.getProperty("url"),
props.getProperty("username"),
props.getProperty("password"));
acturalSize++;
return con;
}else{
long future=System.currentTimeMillis()+mills;
long remaining=mills;
while(connections.isEmpty()&&remaining>0){
connections.wait(remaining);
remaining=future-System.currentTimeMillis();
}
Connection result=null;
if(!connections.isEmpty()){
result=connections.removeFirst();
}
return result;
}
}
}
}
}

配置文件:

driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/acled?rewriteBatchedStatements\=true
username=root
password=123
initSize=5
maxSize=7
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845