一个简单的MySql数据库连接池的实现
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
一个简单的MySql数据库连接池的实现的更多相关文章
- 用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...
- 转 Swoole】用swoole简单实现MySQL连接池
MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...
- 自定义一个简单的JDBC连接池
一.什么是JDBC连接池? 在传统的JDBC连接中,每次获得一个Connection连接都需要加载通过一些繁杂的代码去获取,例如以下代码: public static Connection getCo ...
- Go语言之从0到1实现一个简单的Redis连接池
Go语言之从0到1实现一个简单的Redis连接池 前言 最近学习了一些Go语言开发相关内容,但是苦于手头没有可以练手的项目,学的时候理解不清楚,学过容易忘. 结合之前组内分享时学到的Redis相关知识 ...
- Python实现Mysql数据库连接池
python连接Mysql数据库: python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访 ...
- mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息
1.1. mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息 有时候我们想要获取到数据库的基本信息,当前程序连接的那个数据库,数据库的版本信息,数据库中有哪些表,表中都有什么字段 ...
- MySql数据库连接池专题
MySql数据库连接池专题 - aspirant - 博客园https://www.cnblogs.com/aspirant/p/6747238.html
- python3 实现mysql数据库连接池
首先声明一下,这篇博客进行了通过自己的代码方式,加上这篇博客,最后总结出这段代码.参考博客连接:http://blog.csdn.net/zbc1090549839/article/details/5 ...
- 【数据库开发】如何创建MySQL数据库连接池(一个基于libmysql的MySQL数据库连接池示例(C/C++版))
http://blog.csdn.net/horace20/article/details/8087557 1. 一般架构说明 图 1 架构层次图 一般应用系统数据库访问模块可大致分为两层,一层 ...
随机推荐
- javascript数据结构和算法
一.栈 javascript实现栈的数据结构(借助javascript数组原生的方法即可) //使用javascript来实现栈的数据结构 var Stack={ //不需要外界传参进行初始化,完全可 ...
- 使用Flexible适配移动端html页面 - demo记录
前段时间看了大神的博客文章[使用Flexible实现手淘H5页面的终端适配](地址:http://www.w3cplus.com/mobile/lib-flexible-for-html5-layou ...
- C++之路进阶——codevs3333(高级打字机)
3333 高级打字机 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 早苗入手了最新的高级打字机.最新款自然有着与 ...
- C++之路进阶——bzoj1821(部落划分)
F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser hyxzc Logout 捐赠本站 Notice:1:由于本OJ建 ...
- Windows Phone 十九、罗盘
磁力计概述 拥有磁力计硬件支持的设备可以根据磁力计来确定相对于北极的角度 磁力计的访问 API 定义在 Compass 类中 调用方式和加速计类似 <Grid Background=" ...
- JavaScript 的倒计时
一年前,在网上找到的例子,现在已经找不到出处,对不住原作者,请原谅.修改了一下,在刷新页面的情况下,倒计时不重来. 没有任何样式,纯文字倒计时. <!DOCTYPE html> <h ...
- Meteor全栈开发平台 - 不仅仅是前端
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .网站上有对应每一 ...
- 一周试用yii开发一个带各种该有功能的web程序(二)
上篇随笔写完的是yii能使用简单的命令创建出一个基本的架构,我们只需要在这个架构上进行代码编写,扩展功能.而生成的一个小型系统是可以操作的,但是不是我们想要的,所以,这篇结合源码讲如何创建出我们自己的 ...
- 闲鱼demo
编程是一种美德,是促使一个人不断向上发展的一种原动力 -----–以下是正文------- 最近好多app的底部标签导航使用以下形式了,所以我们就来学习一下它是如何实现的. 先看效果: 中间的&quo ...
- System.map文件【转】
转自:http://blog.csdn.net/david104/article/details/7194185 当运行GNU链接器gld(ld)时若使用了"-M"选项,或者使用n ...