简单的数据库连接池实例(java语言)
1.概述
频繁的创建和销毁数据库连接消耗非常多的系统资源,创建一个池子, 管理一定数量的连接,用的时候去池中取,用完了放回池中,这时比较通用的做法。
2.关键字
LinkedList synchronized InvocationHandler CountDownLatch
3. 代码
3.1 ConnectionPool.java
package com.rocky.pool; import java.sql.Connection;
import java.util.LinkedList; public class ConnectionPool { private LinkedList<Connection> pool = new LinkedList<Connection>(); public ConnectionPool(int initialSize){
if(initialSize > 0){
for(int i=0; i<initialSize; i++){
pool.addLast(ConnectionDriver.createConection());
}
}
} public void releaseConnection(Connection connection){
if(connection != null){
synchronized (pool) {
//连接释放后 要进行通知 这样其他消费者能够感知池中已经归还了一个连接
pool.addLast(connection);
// pool.notifyAll();//all
pool.notify();//all }
}
} public Connection fetchConnection(long mills) throws InterruptedException{
synchronized (pool) {
//超时
if(mills <= 0){
while(pool.isEmpty()){
pool.wait();
}
return pool.removeFirst();
}else{
long future = System.currentTimeMillis() + mills;
long remaining = mills;
while(pool.isEmpty() && remaining >0){
pool.wait(remaining);
remaining = future - System.currentTimeMillis();
}
Connection result = null;
if(!pool.isEmpty()){
result = pool.removeFirst();
}
return result;
}
} }
}
3.2 ConnectionDriver.java
package com.rocky.pool; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection; public class ConnectionDriver { static class ConnectionHandler implements InvocationHandler{
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("commit")){
Thread.sleep(1000);
}
return null;
}
} //创建一个connection的代理
public static Connection createConection(){
return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(), new Class<?>[]{Connection.class},new ConnectionHandler());
}
}
3.3 ConnectionPoolTest.java
package com.rocky.pool; import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger; public class ConnectionPoolTest { static ConnectionPool pool = new ConnectionPool(10); //保证所有runner能够同时运行
static CountDownLatch start = new CountDownLatch(1); static CountDownLatch end ; public static void main(String[] args) throws Exception {
int threadCount = 20; end = new CountDownLatch(threadCount); int count = 20;
AtomicInteger got = new AtomicInteger();
AtomicInteger notGot = new AtomicInteger();
for(int i=0; i<threadCount; i++){
Thread thread = new Thread(new ConnectionRunner(count, got, notGot), "ConectionRunnerThread"+i);
thread.start();
}
start.countDown();
end.await();
System.out.println("total invoke: "+ (threadCount) * count);
System.out.println("got connection: "+got);
System.out.println("not got connection "+ notGot);
} static class ConnectionRunner implements Runnable{ int count ;
AtomicInteger got;
AtomicInteger notGot;
public ConnectionRunner(int count, AtomicInteger got, AtomicInteger notGot){
this.count = count;
this.got = got;
this.notGot = notGot;
} @Override
public void run() { try {
start.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
while(count > 0){
try {
Connection connection = pool.fetchConnection(1000);
if(connection != null){
try{
connection.createStatement();
connection.commit();
}finally{
pool.releaseConnection(connection);
got.incrementAndGet();
}
}else{
notGot.incrementAndGet();
}
} catch (InterruptedException | SQLException e) {
e.printStackTrace();
}finally{
count--;
}
}
end.countDown();
} } }
3.4 说明
通过改变main方法中的threadCount的数量可以观察 随着线程数的增加 获取连接命中的比例在下降,
这时因为连接池中的连接数一定(10个) 而客户端线程会等待并超时返回。
简单的数据库连接池实例(java语言)的更多相关文章
- 数据源与JNDI资源实现JSP数据库连接池实例
名词解释:JNDI的全称是java命名与目录接口(Java Naming and Directory Interface),是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通 ...
- [数据库连接池二]Java数据库连接池--C3P0和JDNI.
前言:上一篇文章中讲了DBCP的用法以及实现原理, 这一篇再来说下C3P0和JDNI的用法. 1.1.C3P0数据源 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规 ...
- mongodb数据库连接池(java版)
mongodb数据库接口的设计 package storm.db; import java.util.ArrayList; import com.mongodb.DB; import com.mong ...
- 简单理解数据库连接池(JDBC)
为什么要使用连接池? 在我们写代码的时候,写了很多类,假如这些类都和数据库打交道.这样的话每个类都要去获取数据库连接,操作完了之后就把连接释放了. 要知道,获取数据库连接的操作其实是向操作系统底层去获 ...
- Python数据库连接池实例——PooledDB
不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...
- Python数据库连接池实例——PooledDB
不用连接池的MySQL连接方法 import MySQLdb conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='m ...
- dbcp数据库连接池的java实现
1.准备 导入jar包 commons-dbcp-1.4.jar commons-pool-1.3.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.jar 2 ...
- C3P0数据库连接池的java实现
1.配置准备 导入jar包 c3p0-0.9.2-pre1.jar mchange-commons-0.2.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.j ...
- 第77节:Java中的事务和数据库连接池和DBUtiles
第77节:Java中的事务和数据库连接池和DBUtiles 前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许 ...
随机推荐
- Determining the Size of a Class Object---sizeof(class)---By Girish Shetty
There are many factors that decide the size of an object of a class in C++. These factors are: Size ...
- 对路径访问拒绝,要加上具体filename/c.png
string strPath = Path.Combine(FilePath, DateTime.Now.ToString("yyyy-MM-dd")); if (!Directo ...
- WeakHashMap源码分析
WeakHashMap是一种弱引用map,内部的key会存储为弱引用, 当jvm gc的时候,如果这些key没有强引用存在的话,会被gc回收掉, 下一次当我们操作map的时候会把对应的Entry整个删 ...
- Codeforces Round #556 题解
Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...
- js的节流和防抖
1,节流 节流就是对连续的函数触发,在设定的间隔时间段内,只让其执行一次. 先来看看js高级程序设计3里面节流的代码 function throttle (method, context, wait) ...
- odoo 二次开发的一些原理
一:self是什么 目前新版的Odoo中使用到的self,是对 游标cr.用户ID.模型.上下文.记录集.缓存 的封装. 我们可以通过 self.XX 获取到这些封装的东西,比如:self.cr. ...
- iOS开发苹果内购的介绍与实现
1.iOS开发苹果内购的介绍 1.1 介绍 苹果规定,凡是虚拟的物品(例如:QQ音乐的乐币)进行交易时,都必须走苹果的内购通道,苹果要收取大约30%的抽成,所以不允许接入第三方的支付方式(微信.支付宝 ...
- Mysql+innodb数据存储逻辑
Mysql+innodb数据存储逻辑. 表空间由段,区,页组成 ibdata1:共享表空间.即所有的数据都存放在这个表空间内.如果用户启用了innodb_file_per_table,则每张表内的数据 ...
- IO流(一)字节流
1:io流体系:对数据进行读写操作.所以IO不是读就是写咯. 2:io流的相关的类:java.io包. 有关IO的操作都会产生IOException异常 3:io:参照物是程序, i:input.进来 ...
- 为easyui添加多条件验证
easyui的验证框架,validatebox不能有效的支持多个条件的验证,比如中文用户名,既要验证其是中文,又要验证其长度不超过6位时便显得很繁琐,需要反复的为easyui添加验证规则. 在此实现一 ...