简单的数据库连接池实例(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个) 而客户端线程会等待并超时返回。

时间: 2024-07-30 02:57:57

简单的数据库连接池实例(java语言)的相关文章

【java】简单实现数据库连接池

一直在想java事务是怎么实现的,在原声jdbc的时候级别下,我们可以通过关掉autocommit 然后再手动commit.但是项目开发中基本上是看不见conection的.所以自己决定简单实现框架的一点皮毛功能.首先就是数据库连接池了 1. 先定义一个接口 import java.sql.Connection; public interface IConnectionPool { /** * 获取一个连接 * @return */ Connection getConnection(); /**

[数据库连接池二]Java数据库连接池--C3P0和JDNI.

前言:上一篇文章中讲了DBCP的用法以及实现原理, 这一篇再来说下C3P0和JDNI的用法. 1.1.C3P0数据源 C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等.C3P0数据源在项目开发中使用得比较多. c3p0与dbcp区别 dbcp没有自动回收空闲连接的功能 c3p0有自动回收空闲连接功能 1.2.在应用程序中加入C3P0连接池 1.导入相关jar包    c3p0-0.

自己实现的一个简单的数据库连接池

自己突然就找到了以前学习的感觉,要不是完了这几个月英爱能找一份比现在好点的工作,说实话还是有点后悔,但是没关系,从现在开始加油还来得及. 今天首先学的是数据库连接,就想到了实现一个数据库连接池,虽然很Lower 但是还是记录下来,一步一个脚印. 首先实现方法是创建一个连接池对象,里面放一个List对象用来保存数据库连接,自己写一个类,实现Connection对象,在里面的close方法里面将连接还给连接池. 然后在从数据库连接池中获取数据的时候用removeFirst方法,移除并返回第一个对象,

mongodb数据库连接池(java版)

我用的java驱动为2.13版本 驱动的下载地址为:http://mongodb.github.io/mongo-java-driver/ 对应的API地址为:http://api.mongodb.org/java/2.13/ mongodb数据库接口的设计 package storm.db; import java.util.ArrayList; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb

JAVA写简单的数据库连接池

创建数据库连接以及关闭连接是很耗费时间的,并且数据库支持的连接数量也是有限的,当数据库的连接数量达到上限的时候,后续的连接就会失败.因此这里引入了数据库缓冲池. public class ConnecionPool { private int size; List<Connection> connections = new ArrayList<>(); public ConnecionPool(int size){ this.size=size; init(); } public

Python数据库连接池实例——PooledDB

不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306) cur=conn.cursor() SQL="select * from table1" r=cur.execute(SQL) r=cur.fetchall() cur.close() conn.close() 用连接池后的连接方法 import MySQLd

Python数据库连接池实例&mdash;&mdash;PooledDB

不用连接池的MySQL连接方法 import MySQLdb conn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='myDB',port=3306) cur=conn.cursor() SQL="select * from table1" r=cur.execute(SQL) r=cur.fetchall() cur.close() conn.close() 用连接池后的连接方法 import MySQL

第77节:Java中的事务和数据库连接池和DBUtiles

前言 看哭你,字数:8803,承蒙关照,谢谢朋友点赞! 事务 Transaction事务,什么是事务,事务是包含一组操作,这组操作里面包含许多个单一的逻辑,只要有一个逻辑没有执行成功就算失败,导致回滚就是指所有的数据都会回到最初的状态. 有事务,是为了保证逻辑一定要成功,如银行转账. 回顾一下 什么是jsp,jsp的三大指令. page: 定义当前页面的信息 include: 包含其他页面 taglib: 引入标签库 三大动作标签: <jsp:forward page="">

[数据库连接池] Java数据库连接池--DBCP浅析.

前言对于数据库连接池, 想必大家都已经不再陌生, 这里仅仅设计Java中的两个常用数据库连接池: DBCP和C3P0(后续会更新). 一. 为何要使用数据库连接池假设网站一天有很大的访问量,数据库服务器就需要为每次连接创建一次数据库连接,极大的浪费数据库的资源,并且极易造成数据库服务器内存溢出.拓机.数据库连接是一种关键的有限的昂贵的资源,这一点在多用户的网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标.数据库连接池正式针对这个问题提