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数据库连接池的实现的更多相关文章

  1. 用swoole简单实现MySQL连接池

    MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...

  2. 转 Swoole】用swoole简单实现MySQL连接池

    MySQL连接池 在传统的网站开发中,比如LNMP模式,由Nginx的master进程接收请求然后分给多个worker进程,每个worker进程再链接php-fpm的master进程,php-fpm再 ...

  3. 自定义一个简单的JDBC连接池

    一.什么是JDBC连接池? 在传统的JDBC连接中,每次获得一个Connection连接都需要加载通过一些繁杂的代码去获取,例如以下代码: public static Connection getCo ...

  4. Go语言之从0到1实现一个简单的Redis连接池

    Go语言之从0到1实现一个简单的Redis连接池 前言 最近学习了一些Go语言开发相关内容,但是苦于手头没有可以练手的项目,学的时候理解不清楚,学过容易忘. 结合之前组内分享时学到的Redis相关知识 ...

  5. Python实现Mysql数据库连接池

    python连接Mysql数据库: python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访 ...

  6. mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息

    1.1. mysql数据库连接池使用(三)数据库元数据信息反射数据库获取数据库信息 有时候我们想要获取到数据库的基本信息,当前程序连接的那个数据库,数据库的版本信息,数据库中有哪些表,表中都有什么字段 ...

  7. MySql数据库连接池专题

    MySql数据库连接池专题 - aspirant - 博客园https://www.cnblogs.com/aspirant/p/6747238.html

  8. python3 实现mysql数据库连接池

    首先声明一下,这篇博客进行了通过自己的代码方式,加上这篇博客,最后总结出这段代码.参考博客连接:http://blog.csdn.net/zbc1090549839/article/details/5 ...

  9. 【数据库开发】如何创建MySQL数据库连接池(一个基于libmysql的MySQL数据库连接池示例(C/C++版))

      http://blog.csdn.net/horace20/article/details/8087557 1.  一般架构说明 图 1 架构层次图 一般应用系统数据库访问模块可大致分为两层,一层 ...

随机推荐

  1. Testlink与Redmine关联

    TestLink是一个开源的测试管理工具,它可以有效地管理整个测试流程(测试需求, 测试计划, 测试用例, 测试执行, 测试结果分析),但不能和开发流程统一起来,从而不能及时参与到开发中去,不能使项目 ...

  2. JSP三大指令、七大动作、九大对象

    <%---------------------- JSP三大指令 -----------------------------%><%-- 1. page: language impo ...

  3. 最新版ssh hibernate spring struts2环境搭建

    最新版ssh hibernate spring struts2环境搭建 最新版spring Framework下载地址:spring4.0.0RELEASE环境搭建 http://repo.sprin ...

  4. Leetcode: Line Reflection

    Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...

  5. Webform 文件上传、 C#加图片水印 、 图片验证码

    文件上传:要使用控件 - FileUpload 1.如何判断是否选中文件? FileUpload.FileName - 选中文件的文件名,如果长度不大于0,那么说明没选中任何文件 js - f.val ...

  6. mac上启动Java项目失败

    解决办法参考地址:http://bbs.csdn.net/topics/390813742,感谢csdn账号为iwordword的大神

  7. memcpy内存复制

    memcpy(predata,frame,1920*1080*4);

  8. .NET中表单的JS验证

    JS验证代码如下:(需要引入两个JS包) <script type="text/javascript" src="/js/jquery.validate.min.j ...

  9. C#压缩库SharpZipLib的应用

       SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤. ...

  10. nginx,文件下载,预览,防止浏览器下载时直接打开,防止预览时直接下载文件,解决nginx谷歌浏览器不支持下载问题

    公司项目逐渐增多,对效率的要求越来越高,不同项目分部不同服务器,最初想用nginx 就是为了多个项目用一个url和服务器宕机解决方案 nginx也可作为附件服务器,毕竟nginx也对静态文件支持较好, ...