一个简单的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 架构层次图 一般应用系统数据库访问模块可大致分为两层,一层 ...
随机推荐
- RSA加密
1.RSA的公钥和私钥到底哪个才是用来加密和哪个用来解密? 答:公钥加密私钥可解,私钥加密公钥可解. 2.RSA非对称加密特点? 答:算法强度复杂.加密解密速度比对称加密解密的速度慢.一个公钥,对外开 ...
- windows下面配置apache+http
一.apache安装 下载并安装apache_2.2.9-win32-x86-openssl-0.9.8h-r2.msi(见附件),找到apache安装目录(C:\Program Files (x86 ...
- sed 引入shell变量
双单引号即可 1.eval sed ’s/$a/$b/’ filename2.sed "s/$a/$b/" filename3.sed ’s/’$a’/’$b’/’ filenam ...
- Issue 0:发刊词
最近读吴军博士的文章,很受感悟.知识的成体系地积累过程对一个人的素养提高很有帮助,所以打算开通这本电子期刊,以一周一篇文章的形式汇总今后的知识体系. 宗旨:及时和团队讨论,反馈:善于利用工具.时间越长 ...
- SQL SELECT INTO使用
SQL SELECT INTO 语句可用于创建表的备份复件. SELECT INTO 语句 SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中. SELECT INTO 语句常用 ...
- UE4入门与精通
由于目前在使用UE4引擎,多少也有一些心得,比如在日常使用中会遇到一些问题.坑(潜规则)或者一些使用技巧等.本人决定开一个大坑,主要有两个目的:一是可以自己做个记录,二是可以给大家提供一些参考吧.主要 ...
- [Android Tips] 22. Available Java 7 Features in Android
This only allows Java 7 language features, and you can hardly benefit from anything since a half of ...
- Add&Delete WindowService
Part One-Add: Step4: Add the new service to windows service: $commandLine = 'D:\IMS\2.16.0.42-DataSe ...
- LeetCode:Two Sum II
public class Solution { public int[] twoSum(int[] numbers, int target) { int left = 0; int right = n ...
- union和union all有什么不同?
union和union all有什么不同? 相同点:用来获取两个或者两个以上结果集的并集 不同点: union会自动去重,排序 union all没有去重,排序