一个简单的数据库连接池

一个简单的数据库连接池

这些阵子正在看《java并发编程的艺术》这本书,感觉还不错,看到多线程等待超时唤醒的时候,看到了这个例子,里面涉及到了几个亮点的技术,在这里记录一下

创建连接池

package com.kangxin.doctor.connectdemo;

import java.sql.Connection;
import java.util.LinkedList;

public class ConnectionPool {
    private LinkedList<Connection> pool = new LinkedList<>();

    public ConnectionPool(int initialSize){
        if (initialSize > 0){
            for (int i=0; i < initialSize; i++){
                pool.addLast(ConnectionDriver.createConnection());
            }
        }
    }

    public void releaseConnection(Connection connection){
        if (connection != null){
            synchronized (pool){
                //连接释放后需要进行通知,这样其他消费者能够感知到连接池中已经归还了一个连接
                pool.addLast(connection);
                pool.notifyAll();
            }
        }
    }

    /**
     * 在mills 内无法获取到连接,将会返回null
     * @param mills
     * @return
     * @throws InterruptedException
     */
    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;
            }
        }
    }

}

可以看到,首先创建了一个LinkedList的连接池集合,通过构造方法传入连接池的数量,然后循环依次创建连接池放入集合中;通过fetchConnection从集合中获取一个连接如果在mills 内无法获取到连接,将会返回null;通过releaseConnection来释放连接,其实就是将连接重新放入集合中。

连接驱动

package com.kangxin.doctor.connectdemo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.concurrent.TimeUnit;

public class ConnectionDriver {

    static class ConnectHandler implements InvocationHandler{

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            if (method.getName().equals("commit")){
                TimeUnit.MILLISECONDS.sleep(100);
            }
            return null;
        }
    }

    /**
     * 创建一个Connection的代理,在commit时休眠100毫秒
     * @return
     */
    public static final Connection createConnection(){
        return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(),
                new Class<?>[] {Connection.class}, new ConnectHandler());
    }
}

此类主要是通过动态代理将Connection接口实现一下来创建一个连接(Connection),并且如果调用了commit方法,则等待100mills。

连接池测试

package com.kangxin.doctor.connectdemo;

import java.sql.Connection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class ConnectionPoolTest {
    static ConnectionPool pool = new ConnectionPool(10);
    //保证所有ConnectionRunner能够同时开始
    static CountDownLatch start = new CountDownLatch(1);
    //main 线程将会等待所有ConnecionRunner结束后才能继续执行
    static CountDownLatch end;

    public static void main(String[] args) throws InterruptedException {
        //线程数量,可以修改线程数量进行观察

        int threadCount = 10;

        end = new CountDownLatch(threadCount);

        int count = 20;

        AtomicInteger got = new AtomicInteger();
        AtomicInteger noGot = new AtomicInteger();

        for (int i=0; i < threadCount; i++){
            Thread thread = new Thread(new ConnectionRunner(count, got, noGot), "ConnectionRunnerThread");
            thread.start();
        }

        //同时执行多个线程
        start.countDown();
        end.await();
        System.out.println("total invoke:" + (threadCount * count));
        System.out.println("got connecion:" + got);
        System.out.println("not got connecion:" + noGot);
    }

    static class ConnectionRunner implements Runnable{

        int count;
        AtomicInteger got;
        AtomicInteger noGot;

        public ConnectionRunner(int count, AtomicInteger got, AtomicInteger noGot) {
            this.count = count;
            this.got = got;
            this.noGot = noGot;
        }

        @Override
        public void run() {
            try {
                start.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            while (count > 0){
                try {
                    //从线程池中获取连接,如果1000ms内无法获取到,将会返回null
                    //分别统计连接获取的数量got和未获取到的数量noGot
                    Connection connection = pool.fetchConnection(1000);

                    if (connection != null){
                        try {
                            connection.createStatement();
                            connection.commit();
                        }finally {
                            pool.releaseConnection(connection);
                            got.incrementAndGet();
                        }
                    }else {
                        noGot.incrementAndGet();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    count -- ;
                }
            }

            end.countDown();
        }
    }
}

可以看到在main线程中创建了10个线程,每个线程都会获取20个连接,然后通过CountDownLatch的控制,可以让这10个线程同时执行获取连接的操作;以下是程序的运行结果:

total invoke:200
got connecion:200
not got connecion:0

可以看到连接全部获取到了

亮点介绍

  • 动态代理
    用动态代理去创建连接我们不用去关心连接的具体实现,只关心接口就行了, 这在sdk与框架设计中起到了举足轻重的作用比如android网络请求框架Retrofit
  • CountDownLatch 与AtomicInteger的用法
    1. CountDownLatch
      CountDownLatch能够使一个或多个线程等待其他线程完成各自的工作后再执行, 它类似于多线程计数器的所用。在实例化的时候通过传入计数的数量值来对多线程同步作控制,在本例子中end的实例化计数器数量为10,因为有10个线程,在main线程end.await(); 这个时候main线程会等待其他线程执行完之后再执行,在子线程的run方法最后end.countDown(); 也就是说每执行一个子线程计数器减1,如果将刚开始实例化时传入的计数器的值减完之后,程序会继续执行main线程的操作。
    2. AtomicInteger
      这是一个原子操作类,对于Java中的运算操作,例如自增或自减,若没有进行额外的同步操作,在多线程环境下就是线程不安全的。原子类保证了在多线程情况下线程的安全性,换句话说就是在多线程操作的时候,当前线程作原子类计算,其他线程不会去改变这个原子类的值,等到当前线程执行完之后才可以,也就是说当前线程在执行完原子类操作之后,原子类的值对其他线程都是可见的。

以上是我所了解到的东西,若说的有不当指出,还请网友指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值