数据库连接池的简单实现

<pre name="code" class="java">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 ConnectionHandler implements InvocationHandler
    {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
        {
            if (method.getName().equals("commit"))
            {
                TimeUnit.MILLISECONDS.sleep(200);
            }
            return null;
        }
    }
    // 创建一个Connection的代理,在commit时休眠1秒
    public static final Connection createConnection()
    {
        return (Connection) Proxy.newProxyInstance(ConnectionDriver.class.getClassLoader(), new Class<?>[] { Connection.class },
            new ConnectionHandler());
    }
}

连接池
package chapter04;
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.createConnection());
            }
        }
    }
    public void releaseConnection(Connection connection)
    {
        if (connection != null)
        {
            synchronized (pool)
            {
                // 添加后需要进行通知,这样其他消费者能够感知到链接池中已经归还了一个链接
                pool.addLast(connection);
                pool.notifyAll();
            }
        }
    }
    // 在mills内无法获取到连接,将会返回null
    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;
            }
        }
    }
}
public class ConnectionPoolTest {
    static ConnectionPool pool = new ConnectionPool(10);
    // 保证所有ConnectionRunner能够同时开始
    static CountDownLatch start = new CountDownLatch(1);
    // main线程将会等待所有ConnectionRunner结束后才能继续执行
    static CountDownLatch end;
    public static void main(String[] args) throws Exception {
        // 线程数量,可以线程数量进行观察
        int threadCount = 50;
        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 ConnetionRunner(count, got, notGot), "ConnectionRunnerThread");
            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 ConnetionRunner implements Runnable {
        int count;
        AtomicInteger got;
        AtomicInteger notGot;
        public ConnetionRunner(int count, AtomicInteger got, AtomicInteger notGot) {
            this.count = count;
            this.got = got;
            this.notGot = notGot;
        }
        public void run()
        {
            try
            {
                start.await();
            }
            catch (Exception ex)
            {
            }
            while (count > 0)
            {
                try
                {
                    // 从线程池中获取连接,如果1000ms内无法获取到,将会返回null
                    // 分别统计连接获取的数量got和未获取到的数量notGot
                    Connection connection = pool.fetchConnection(1000);
                    if (connection != null)
                    {
                        try
                        {
                            connection.createStatement();
                            connection.commit();
                        }
                        finally
                        {
                            pool.releaseConnection(connection);
                            got.incrementAndGet();
                        }
                    }
                    else
                    {
                        notGot.incrementAndGet();
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    count--;
                }
            }
            end.countDown();
        }
    }
}



                
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值