1. 程式人生 > >簡單的數據庫連接池實例(java語言)

簡單的數據庫連接池實例(java語言)

proxy read public ++ load empty 返回 not 超時

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語言)