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

一直在想java事务是怎么实现的,在原声jdbc的时候级别下,我们可以通过关掉autocommit 然后再手动commit。但是项目开发中基本上是看不见conection的。所以自己决定简单实现框架的一点皮毛功能。首先就是数据库连接池了

1. 先定义一个接口

import java.sql.Connection;

public interface IConnectionPool {

    /**
     * 获取一个连接
     * @return
     */
    Connection getConnection();

    /**
     * 用完后调用,把连接放回池中,实现复用
     */
    void freeLocalConnection();

    /**
     * 销毁连接池
     */
    void destroy();

    //测试用
    void status();

}

2. 实现数据库连接池的代码, 为了线程安全,简单粗暴地用synchronized关键字

  实现事务的关键是,我们执行一个事务的Connection是同一个,我们可以在事务控制的时候用AOP,在事务开始的时候 调用setAutoCommit(false)  然后在事务代码之后调用commit()方法.

  所以在数据库连接池中使用了一个ThreadLocal来保证一条线程拿到的是同一个Connection。

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

public class ConnectionPollImpl implements IConnectionPool{

    private String username;
    private String password;
    private String url;
    private String driver;

    private Integer maxSize;
    private Integer initSize = 5;
    private long timeOut;

    //连接总数
    private AtomicInteger totalSize = new AtomicInteger(0);
    //空闲的连接
    private List<Connection> freeConnections = new Vector<>();
    //已经被使用的连接
    private List<Connection> activeConnections = new Vector<>();
    //存储当前线程的连接, 事务控制的关键
    private ThreadLocal<Connection> localConnection = new ThreadLocal<Connection>(){

        /**
         * 第一次调用get()方法时执行
         * @return
         */
        @Override
        protected Connection initialValue() {
            try {
                return connect();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public void remove() {
            Connection connection = get();
            activeConnections.remove(connection);
            freeConnections.add(connection);
            super.remove();
        }
    };

    private static ConnectionPollImpl instance;

    public ConnectionPollImpl() {
        loadConfig();
        init();
    }

    private void init() {
        try {
            for(int i=0;i < initSize;i++){
                freeConnections.add(newConnection());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public static ConnectionPollImpl getInstance() {
        synchronized (ConnectionPollImpl.class) {
            if (instance == null) {
                synchronized (ConnectionPollImpl.class) {
                    if(instance == null) {
                        instance = new ConnectionPollImpl();
                    }
                }
            }
        }
        return instance;
    }

    @Override
    public synchronized Connection getConnection() {
        return localConnection.get();
    }

    @Override
    public void freeLocalConnection() {
        localConnection.remove();
        System.out.println(Thread.currentThread().getName() + "释放了一个连接");

    }

    @Override
    public synchronized void destroy() {
        try {
            for(Connection connection : freeConnections) {
                freeConnections.remove(connection);
                connection.close();
            }
            freeConnections = null;
            for (Connection connection : activeConnections) {
                activeConnections.remove(connection);
                connection.close();
            }
            activeConnections = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public synchronized void status() {

        System.out.println("当前连接池总连接数为: " + totalSize.get() + " , 空闲连接数为:" + freeConnections.size() + "使用中的连接数为:" + activeConnections.size());

    }

    private synchronized Connection connect() throws SQLException {
        // 判断有没有闲置的连接
        if(freeConnections.size() > 0) {
            //如果有闲置连接,直接拿第一个
            Connection connection = freeConnections.get(0);
            freeConnections.remove(0);
            //连接可用,返回;不可用,继续拿
            if (isValid(connection)) {
                activeConnections.add(connection);
                return connection;
            } else {
                return connect();
            }
        } else {
            //没有闲置连接, 判断当前连接池是否饱和
            if(totalSize.get() == maxSize) {
                //如果饱和,等待, 继续获取
                try {
                    wait(timeOut);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return connect();
            } else {
                //没有饱和,新建一个连接
                Connection connection = newConnection();
                if(connection != null) {
                    activeConnections.add(connection);
                    return connection;
                } else {
                    throw new SQLException();
                }
            }
        }
    }

    private synchronized Connection newConnection() throws SQLException {
        try {
            Class.forName(this.driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection =  DriverManager.getConnection(url, username, password);
        totalSize.incrementAndGet();
        return connection;
    }

    private boolean isValid(Connection connection) {
        try {
            return connection != null && !connection.isClosed();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private void loadConfig(){
        //读取配置文件
        InputStream in = ConnectionPollImpl.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties p = new Properties();
        try {
            p.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.username = p.getProperty("jdbc.username");
        this.password = p.getProperty("jdbc.password");
        this.url = p.getProperty("jdbc.url");
        this.driver = p.getProperty("jdbc.driver");

        this.maxSize = Integer.valueOf(p.getProperty("noob.maxSize","20"));
        this.initSize = Integer.valueOf(p.getProperty("noob.initSize","5"));
        this.timeOut = Long.valueOf(p.getProperty("noob.timeOut","1200"));

    }
}

测试代码

import java.sql.Connection;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Test {

    public static void main(String[] args) {
        IConnectionPool connectionPool = ConnectionPollImpl.getInstance();

        //开启一个线程查看连接池的状态
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        service.scheduleWithFixedDelay(connectionPool::status, 0, 5, TimeUnit.SECONDS);

        //开启20个线程,不断获取连接,比较哈希值看同一个线程取出的连接是不是同一个
        for(int i = 0; i < 20; i++) {
            Random random = new Random();
            int count = random.nextInt(30) + 3;
            Thread t = new Thread(() ->{
                try {
                    for (int j = 0; j < count; j++) {
                        Connection connection = connectionPool.getConnection();

                        System.out.println(Thread.currentThread().getName() + "共" + count + "次循环, 目前第" + (j + 1) + "次" + " hashcode :" + connection.hashCode());
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                connectionPool.freeLocalConnection();
            });
            t.setName("test" + i);
            t.start();
        }
    }
}

测试结果, 从结果看基本实现了想要的功能

  1. 控制连接池的大小

  2. 一个线程释放一个连接后会把连接放回池中给别的线程用

  3. 一个线程始终取出同一个连接

当前连接池总连接数为: 5 , 空闲连接数为:5使用中的连接数为:0
test6共22次循环, 目前第1次 hashcode :691902360
test2共18次循环, 目前第1次 hashcode :708075980
test3共16次循环, 目前第1次 hashcode :1535444742
test4共25次循环, 目前第1次 hashcode :1149790650
test5共15次循环, 目前第1次 hashcode :1825737020
test1共16次循环, 目前第1次 hashcode :2094482202
test0共21次循环, 目前第1次 hashcode :889774551
test18共16次循环, 目前第1次 hashcode :1626524709
test11共17次循环, 目前第1次 hashcode :912223199
test19共28次循环, 目前第1次 hashcode :422379330
test3共16次循环, 目前第2次 hashcode :1535444742
test2共18次循环, 目前第2次 hashcode :708075980
test6共22次循环, 目前第2次 hashcode :691902360
test5共15次循环, 目前第2次 hashcode :1825737020
test4共25次循环, 目前第2次 hashcode :1149790650
test1共16次循环, 目前第2次 hashcode :2094482202
test0共21次循环, 目前第2次 hashcode :889774551
test18共16次循环, 目前第2次 hashcode :1626524709
test11共17次循环, 目前第2次 hashcode :912223199
test19共28次循环, 目前第2次 hashcode :422379330
test2共18次循环, 目前第3次 hashcode :708075980
test4共25次循环, 目前第3次 hashcode :1149790650
test3共16次循环, 目前第3次 hashcode :1535444742
test6共22次循环, 目前第3次 hashcode :691902360
test5共15次循环, 目前第3次 hashcode :1825737020
test1共16次循环, 目前第3次 hashcode :2094482202
test0共21次循环, 目前第3次 hashcode :889774551
test18共16次循环, 目前第3次 hashcode :1626524709
test11共17次循环, 目前第3次 hashcode :912223199
test19共28次循环, 目前第3次 hashcode :422379330
test5共15次循环, 目前第4次 hashcode :1825737020
test2共18次循环, 目前第4次 hashcode :708075980
test6共22次循环, 目前第4次 hashcode :691902360
test3共16次循环, 目前第4次 hashcode :1535444742
test4共25次循环, 目前第4次 hashcode :1149790650
test1共16次循环, 目前第4次 hashcode :2094482202
test0共21次循环, 目前第4次 hashcode :889774551
test18共16次循环, 目前第4次 hashcode :1626524709
test11共17次循环, 目前第4次 hashcode :912223199
test19共28次循环, 目前第4次 hashcode :422379330
test3共16次循环, 目前第5次 hashcode :1535444742
test5共15次循环, 目前第5次 hashcode :1825737020
test6共22次循环, 目前第5次 hashcode :691902360
test4共25次循环, 目前第5次 hashcode :1149790650
test2共18次循环, 目前第5次 hashcode :708075980
test1共16次循环, 目前第5次 hashcode :2094482202
test0共21次循环, 目前第5次 hashcode :889774551
test18共16次循环, 目前第5次 hashcode :1626524709
test11共17次循环, 目前第5次 hashcode :912223199
test19共28次循环, 目前第5次 hashcode :422379330
test3共16次循环, 目前第6次 hashcode :1535444742
test5共15次循环, 目前第6次 hashcode :1825737020
test4共25次循环, 目前第6次 hashcode :1149790650
test2共18次循环, 目前第6次 hashcode :708075980
test6共22次循环, 目前第6次 hashcode :691902360
当前连接池总连接数为: 10 , 空闲连接数为:0使用中的连接数为:10
test1共16次循环, 目前第6次 hashcode :2094482202
test0共21次循环, 目前第6次 hashcode :889774551
test18共16次循环, 目前第6次 hashcode :1626524709
test11共17次循环, 目前第6次 hashcode :912223199
test19共28次循环, 目前第6次 hashcode :422379330
test2共18次循环, 目前第7次 hashcode :708075980
test4共25次循环, 目前第7次 hashcode :1149790650
test6共22次循环, 目前第7次 hashcode :691902360
test3共16次循环, 目前第7次 hashcode :1535444742
test5共15次循环, 目前第7次 hashcode :1825737020
test1共16次循环, 目前第7次 hashcode :2094482202
test0共21次循环, 目前第7次 hashcode :889774551
test18共16次循环, 目前第7次 hashcode :1626524709
test11共17次循环, 目前第7次 hashcode :912223199
test19共28次循环, 目前第7次 hashcode :422379330
test3共16次循环, 目前第8次 hashcode :1535444742
test5共15次循环, 目前第8次 hashcode :1825737020
test2共18次循环, 目前第8次 hashcode :708075980
test6共22次循环, 目前第8次 hashcode :691902360
test4共25次循环, 目前第8次 hashcode :1149790650
test1共16次循环, 目前第8次 hashcode :2094482202
test0共21次循环, 目前第8次 hashcode :889774551
test18共16次循环, 目前第8次 hashcode :1626524709
test11共17次循环, 目前第8次 hashcode :912223199
test19共28次循环, 目前第8次 hashcode :422379330
test5共15次循环, 目前第9次 hashcode :1825737020
test4共25次循环, 目前第9次 hashcode :1149790650
test3共16次循环, 目前第9次 hashcode :1535444742
test2共18次循环, 目前第9次 hashcode :708075980
test6共22次循环, 目前第9次 hashcode :691902360
test1共16次循环, 目前第9次 hashcode :2094482202
test0共21次循环, 目前第9次 hashcode :889774551
test18共16次循环, 目前第9次 hashcode :1626524709
test11共17次循环, 目前第9次 hashcode :912223199
test19共28次循环, 目前第9次 hashcode :422379330
test5共15次循环, 目前第10次 hashcode :1825737020
test6共22次循环, 目前第10次 hashcode :691902360
test3共16次循环, 目前第10次 hashcode :1535444742
test2共18次循环, 目前第10次 hashcode :708075980
test4共25次循环, 目前第10次 hashcode :1149790650
test1共16次循环, 目前第10次 hashcode :2094482202
test0共21次循环, 目前第10次 hashcode :889774551
test18共16次循环, 目前第10次 hashcode :1626524709
test11共17次循环, 目前第10次 hashcode :912223199
test19共28次循环, 目前第10次 hashcode :422379330
当前连接池总连接数为: 10 , 空闲连接数为:0使用中的连接数为:10
test5共15次循环, 目前第11次 hashcode :1825737020
test3共16次循环, 目前第11次 hashcode :1535444742
test6共22次循环, 目前第11次 hashcode :691902360
test4共25次循环, 目前第11次 hashcode :1149790650
test2共18次循环, 目前第11次 hashcode :708075980
test1共16次循环, 目前第11次 hashcode :2094482202
test0共21次循环, 目前第11次 hashcode :889774551
test18共16次循环, 目前第11次 hashcode :1626524709
test11共17次循环, 目前第11次 hashcode :912223199
test19共28次循环, 目前第11次 hashcode :422379330
test2共18次循环, 目前第12次 hashcode :708075980
test5共15次循环, 目前第12次 hashcode :1825737020
test3共16次循环, 目前第12次 hashcode :1535444742
test6共22次循环, 目前第12次 hashcode :691902360
test4共25次循环, 目前第12次 hashcode :1149790650
test1共16次循环, 目前第12次 hashcode :2094482202
test0共21次循环, 目前第12次 hashcode :889774551
test18共16次循环, 目前第12次 hashcode :1626524709
test11共17次循环, 目前第12次 hashcode :912223199
test19共28次循环, 目前第12次 hashcode :422379330
test6共22次循环, 目前第13次 hashcode :691902360
test2共18次循环, 目前第13次 hashcode :708075980
test3共16次循环, 目前第13次 hashcode :1535444742
test5共15次循环, 目前第13次 hashcode :1825737020
test4共25次循环, 目前第13次 hashcode :1149790650
test1共16次循环, 目前第13次 hashcode :2094482202
test0共21次循环, 目前第13次 hashcode :889774551
test18共16次循环, 目前第13次 hashcode :1626524709
test11共17次循环, 目前第13次 hashcode :912223199
test19共28次循环, 目前第13次 hashcode :422379330
test3共16次循环, 目前第14次 hashcode :1535444742
test5共15次循环, 目前第14次 hashcode :1825737020
test6共22次循环, 目前第14次 hashcode :691902360
test4共25次循环, 目前第14次 hashcode :1149790650
test2共18次循环, 目前第14次 hashcode :708075980
test1共16次循环, 目前第14次 hashcode :2094482202
test0共21次循环, 目前第14次 hashcode :889774551
test18共16次循环, 目前第14次 hashcode :1626524709
test11共17次循环, 目前第14次 hashcode :912223199
test19共28次循环, 目前第14次 hashcode :422379330
test2共18次循环, 目前第15次 hashcode :708075980
test5共15次循环, 目前第15次 hashcode :1825737020
test4共25次循环, 目前第15次 hashcode :1149790650
test6共22次循环, 目前第15次 hashcode :691902360
test3共16次循环, 目前第15次 hashcode :1535444742
test1共16次循环, 目前第15次 hashcode :2094482202
test0共21次循环, 目前第15次 hashcode :889774551
test18共16次循环, 目前第15次 hashcode :1626524709
test11共17次循环, 目前第15次 hashcode :912223199
test19共28次循环, 目前第15次 hashcode :422379330
当前连接池总连接数为: 10 , 空闲连接数为:0使用中的连接数为:10
test6共22次循环, 目前第16次 hashcode :691902360
test4共25次循环, 目前第16次 hashcode :1149790650
test5释放了一个连接
test3共16次循环, 目前第16次 hashcode :1535444742
test2共18次循环, 目前第16次 hashcode :708075980
test1共16次循环, 目前第16次 hashcode :2094482202
test0共21次循环, 目前第16次 hashcode :889774551
test18共16次循环, 目前第16次 hashcode :1626524709
test11共17次循环, 目前第16次 hashcode :912223199
test19共28次循环, 目前第16次 hashcode :422379330
test7共6次循环, 目前第1次 hashcode :1825737020
test2共18次循环, 目前第17次 hashcode :708075980
test3释放了一个连接
test6共22次循环, 目前第17次 hashcode :691902360
test4共25次循环, 目前第17次 hashcode :1149790650
test1释放了一个连接
test0共21次循环, 目前第17次 hashcode :889774551
test18释放了一个连接
test11共17次循环, 目前第17次 hashcode :912223199
test19共28次循环, 目前第17次 hashcode :422379330
test7共6次循环, 目前第2次 hashcode :1825737020
test12共12次循环, 目前第1次 hashcode :2094482202
test15共12次循环, 目前第1次 hashcode :1626524709
test8共31次循环, 目前第1次 hashcode :1535444742
test4共25次循环, 目前第18次 hashcode :1149790650
test2共18次循环, 目前第18次 hashcode :708075980
test6共22次循环, 目前第18次 hashcode :691902360
test0共21次循环, 目前第18次 hashcode :889774551
test11释放了一个连接
test19共28次循环, 目前第18次 hashcode :422379330
test7共6次循环, 目前第3次 hashcode :1825737020
test8共31次循环, 目前第2次 hashcode :1535444742
test12共12次循环, 目前第2次 hashcode :2094482202
test15共12次循环, 目前第2次 hashcode :1626524709
test4共25次循环, 目前第19次 hashcode :1149790650
test6共22次循环, 目前第19次 hashcode :691902360
test2释放了一个连接
test0共21次循环, 目前第19次 hashcode :889774551
test13共30次循环, 目前第1次 hashcode :912223199
test19共28次循环, 目前第19次 hashcode :422379330
test14共27次循环, 目前第1次 hashcode :708075980
test7共6次循环, 目前第4次 hashcode :1825737020
test12共12次循环, 目前第3次 hashcode :2094482202
test15共12次循环, 目前第3次 hashcode :1626524709
test8共31次循环, 目前第3次 hashcode :1535444742
test6共22次循环, 目前第20次 hashcode :691902360
test4共25次循环, 目前第20次 hashcode :1149790650
test0共21次循环, 目前第20次 hashcode :889774551
test13共30次循环, 目前第2次 hashcode :912223199
test19共28次循环, 目前第20次 hashcode :422379330
test14共27次循环, 目前第2次 hashcode :708075980
test7共6次循环, 目前第5次 hashcode :1825737020
test8共31次循环, 目前第4次 hashcode :1535444742
test15共12次循环, 目前第4次 hashcode :1626524709
test12共12次循环, 目前第4次 hashcode :2094482202
当前连接池总连接数为: 10 , 空闲连接数为:0使用中的连接数为:10
test6共22次循环, 目前第21次 hashcode :691902360
test4共25次循环, 目前第21次 hashcode :1149790650
test0共21次循环, 目前第21次 hashcode :889774551
test13共30次循环, 目前第3次 hashcode :912223199
test19共28次循环, 目前第21次 hashcode :422379330
test14共27次循环, 目前第3次 hashcode :708075980
test7共6次循环, 目前第6次 hashcode :1825737020
test12共12次循环, 目前第5次 hashcode :2094482202
test8共31次循环, 目前第5次 hashcode :1535444742
test15共12次循环, 目前第5次 hashcode :1626524709
test4共25次循环, 目前第22次 hashcode :1149790650
test6共22次循环, 目前第22次 hashcode :691902360
test0释放了一个连接
test19共28次循环, 目前第22次 hashcode :422379330
test13共30次循环, 目前第4次 hashcode :912223199
test14共27次循环, 目前第4次 hashcode :708075980
test16共19次循环, 目前第1次 hashcode :889774551
test7释放了一个连接
test8共31次循环, 目前第6次 hashcode :1535444742
test12共12次循环, 目前第6次 hashcode :2094482202
test15共12次循环, 目前第6次 hashcode :1626524709
test4共25次循环, 目前第23次 hashcode :1149790650
test6释放了一个连接
test19共28次循环, 目前第23次 hashcode :422379330
test14共27次循环, 目前第5次 hashcode :708075980
test13共30次循环, 目前第5次 hashcode :912223199
test16共19次循环, 目前第2次 hashcode :889774551
test12共12次循环, 目前第7次 hashcode :2094482202
test9共15次循环, 目前第1次 hashcode :691902360
test17共15次循环, 目前第1次 hashcode :1825737020
test8共31次循环, 目前第7次 hashcode :1535444742
test15共12次循环, 目前第7次 hashcode :1626524709
test4共25次循环, 目前第24次 hashcode :1149790650
test19共28次循环, 目前第24次 hashcode :422379330
test14共27次循环, 目前第6次 hashcode :708075980
test13共30次循环, 目前第6次 hashcode :912223199
test16共19次循环, 目前第3次 hashcode :889774551
test8共31次循环, 目前第8次 hashcode :1535444742
test12共12次循环, 目前第8次 hashcode :2094482202
test17共15次循环, 目前第2次 hashcode :1825737020
test9共15次循环, 目前第2次 hashcode :691902360
test15共12次循环, 目前第8次 hashcode :1626524709
test4共25次循环, 目前第25次 hashcode :1149790650
test19共28次循环, 目前第25次 hashcode :422379330
test13共30次循环, 目前第7次 hashcode :912223199
test14共27次循环, 目前第7次 hashcode :708075980
test16共19次循环, 目前第4次 hashcode :889774551
test12共12次循环, 目前第9次 hashcode :2094482202
test9共15次循环, 目前第3次 hashcode :691902360
test17共15次循环, 目前第3次 hashcode :1825737020
test8共31次循环, 目前第9次 hashcode :1535444742
test15共12次循环, 目前第9次 hashcode :1626524709
当前连接池总连接数为: 10 , 空闲连接数为:0使用中的连接数为:10
test4释放了一个连接
test13共30次循环, 目前第8次 hashcode :912223199
test19共28次循环, 目前第26次 hashcode :422379330
test14共27次循环, 目前第8次 hashcode :708075980
test10共17次循环, 目前第1次 hashcode :1149790650
test16共19次循环, 目前第5次 hashcode :889774551
test9共15次循环, 目前第4次 hashcode :691902360
test12共12次循环, 目前第10次 hashcode :2094482202
test8共31次循环, 目前第10次 hashcode :1535444742
test17共15次循环, 目前第4次 hashcode :1825737020
test15共12次循环, 目前第10次 hashcode :1626524709
test19共28次循环, 目前第27次 hashcode :422379330
test13共30次循环, 目前第9次 hashcode :912223199
test14共27次循环, 目前第9次 hashcode :708075980
test10共17次循环, 目前第2次 hashcode :1149790650
test16共19次循环, 目前第6次 hashcode :889774551
test8共31次循环, 目前第11次 hashcode :1535444742
test17共15次循环, 目前第5次 hashcode :1825737020
test12共12次循环, 目前第11次 hashcode :2094482202
test9共15次循环, 目前第5次 hashcode :691902360
test15共12次循环, 目前第11次 hashcode :1626524709
test14共27次循环, 目前第10次 hashcode :708075980
test13共30次循环, 目前第10次 hashcode :912223199
test19共28次循环, 目前第28次 hashcode :422379330
test10共17次循环, 目前第3次 hashcode :1149790650
test16共19次循环, 目前第7次 hashcode :889774551
test9共15次循环, 目前第6次 hashcode :691902360
test12共12次循环, 目前第12次 hashcode :2094482202
test8共31次循环, 目前第12次 hashcode :1535444742
test17共15次循环, 目前第6次 hashcode :1825737020
test15共12次循环, 目前第12次 hashcode :1626524709
test14共27次循环, 目前第11次 hashcode :708075980
test19释放了一个连接
test13共30次循环, 目前第11次 hashcode :912223199
test10共17次循环, 目前第4次 hashcode :1149790650
test16共19次循环, 目前第8次 hashcode :889774551
test9共15次循环, 目前第7次 hashcode :691902360
test12释放了一个连接
test17共15次循环, 目前第7次 hashcode :1825737020
test8共31次循环, 目前第13次 hashcode :1535444742
test15释放了一个连接
test13共30次循环, 目前第12次 hashcode :912223199
test14共27次循环, 目前第12次 hashcode :708075980
test10共17次循环, 目前第5次 hashcode :1149790650
test16共19次循环, 目前第9次 hashcode :889774551
test9共15次循环, 目前第8次 hashcode :691902360
test8共31次循环, 目前第14次 hashcode :1535444742
test17共15次循环, 目前第8次 hashcode :1825737020
当前连接池总连接数为: 10 , 空闲连接数为:3使用中的连接数为:7
test14共27次循环, 目前第13次 hashcode :708075980
test13共30次循环, 目前第13次 hashcode :912223199
test10共17次循环, 目前第6次 hashcode :1149790650
test16共19次循环, 目前第10次 hashcode :889774551
test8共31次循环, 目前第15次 hashcode :1535444742
test17共15次循环, 目前第9次 hashcode :1825737020
test9共15次循环, 目前第9次 hashcode :691902360
test14共27次循环, 目前第14次 hashcode :708075980
test13共30次循环, 目前第14次 hashcode :912223199
test10共17次循环, 目前第7次 hashcode :1149790650
test16共19次循环, 目前第11次 hashcode :889774551
test17共15次循环, 目前第10次 hashcode :1825737020
test8共31次循环, 目前第16次 hashcode :1535444742
test9共15次循环, 目前第10次 hashcode :691902360
test13共30次循环, 目前第15次 hashcode :912223199
test14共27次循环, 目前第15次 hashcode :708075980
test10共17次循环, 目前第8次 hashcode :1149790650
test16共19次循环, 目前第12次 hashcode :889774551
test8共31次循环, 目前第17次 hashcode :1535444742
test9共15次循环, 目前第11次 hashcode :691902360
test17共15次循环, 目前第11次 hashcode :1825737020
test13共30次循环, 目前第16次 hashcode :912223199
test14共27次循环, 目前第16次 hashcode :708075980
test10共17次循环, 目前第9次 hashcode :1149790650
test16共19次循环, 目前第13次 hashcode :889774551
test17共15次循环, 目前第12次 hashcode :1825737020
test8共31次循环, 目前第18次 hashcode :1535444742
test9共15次循环, 目前第12次 hashcode :691902360
test13共30次循环, 目前第17次 hashcode :912223199
test14共27次循环, 目前第17次 hashcode :708075980
test10共17次循环, 目前第10次 hashcode :1149790650
test16共19次循环, 目前第14次 hashcode :889774551
test9共15次循环, 目前第13次 hashcode :691902360
test8共31次循环, 目前第19次 hashcode :1535444742
test17共15次循环, 目前第13次 hashcode :1825737020
当前连接池总连接数为: 10 , 空闲连接数为:3使用中的连接数为:7
test13共30次循环, 目前第18次 hashcode :912223199
test14共27次循环, 目前第18次 hashcode :708075980
test10共17次循环, 目前第11次 hashcode :1149790650
test16共19次循环, 目前第15次 hashcode :889774551
test8共31次循环, 目前第20次 hashcode :1535444742
test9共15次循环, 目前第14次 hashcode :691902360
test17共15次循环, 目前第14次 hashcode :1825737020
test14共27次循环, 目前第19次 hashcode :708075980
test13共30次循环, 目前第19次 hashcode :912223199
test10共17次循环, 目前第12次 hashcode :1149790650
test16共19次循环, 目前第16次 hashcode :889774551
test8共31次循环, 目前第21次 hashcode :1535444742
test9共15次循环, 目前第15次 hashcode :691902360
test17共15次循环, 目前第15次 hashcode :1825737020
test13共30次循环, 目前第20次 hashcode :912223199
test14共27次循环, 目前第20次 hashcode :708075980
test10共17次循环, 目前第13次 hashcode :1149790650
test16共19次循环, 目前第17次 hashcode :889774551
test8共31次循环, 目前第22次 hashcode :1535444742
test17释放了一个连接
test9释放了一个连接
test13共30次循环, 目前第21次 hashcode :912223199
test14共27次循环, 目前第21次 hashcode :708075980
test10共17次循环, 目前第14次 hashcode :1149790650
test16共19次循环, 目前第18次 hashcode :889774551
test8共31次循环, 目前第23次 hashcode :1535444742
test13共30次循环, 目前第22次 hashcode :912223199
test14共27次循环, 目前第22次 hashcode :708075980
test10共17次循环, 目前第15次 hashcode :1149790650
test16共19次循环, 目前第19次 hashcode :889774551
test8共31次循环, 目前第24次 hashcode :1535444742
当前连接池总连接数为: 10 , 空闲连接数为:5使用中的连接数为:5
test14共27次循环, 目前第23次 hashcode :708075980
test13共30次循环, 目前第23次 hashcode :912223199
test10共17次循环, 目前第16次 hashcode :1149790650
test16释放了一个连接
test8共31次循环, 目前第25次 hashcode :1535444742
test13共30次循环, 目前第24次 hashcode :912223199
test14共27次循环, 目前第24次 hashcode :708075980
test10共17次循环, 目前第17次 hashcode :1149790650
test8共31次循环, 目前第26次 hashcode :1535444742
test14共27次循环, 目前第25次 hashcode :708075980
test13共30次循环, 目前第25次 hashcode :912223199
test10释放了一个连接
test8共31次循环, 目前第27次 hashcode :1535444742
test14共27次循环, 目前第26次 hashcode :708075980
test13共30次循环, 目前第26次 hashcode :912223199
test8共31次循环, 目前第28次 hashcode :1535444742
test13共30次循环, 目前第27次 hashcode :912223199
test14共27次循环, 目前第27次 hashcode :708075980
test8共31次循环, 目前第29次 hashcode :1535444742
当前连接池总连接数为: 10 , 空闲连接数为:7使用中的连接数为:3
test13共30次循环, 目前第28次 hashcode :912223199
test14释放了一个连接
test8共31次循环, 目前第30次 hashcode :1535444742
test13共30次循环, 目前第29次 hashcode :912223199
test8共31次循环, 目前第31次 hashcode :1535444742
test13共30次循环, 目前第30次 hashcode :912223199
test8释放了一个连接
test13释放了一个连接
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0
当前连接池总连接数为: 10 , 空闲连接数为:10使用中的连接数为:0

原文地址:https://www.cnblogs.com/yeyeck/p/9532603.html

时间: 2024-07-30 02:58:03

【java】简单实现数据库连接池的相关文章

Java使用独立数据库连接池(DBCP为例)

目前,绝大多数的软件系统都会使用数据库,而在软件构建起来之后,访问数据库又成为软件系统性能的短板(I/O操作).一般来说一次访问数据库就需要一个数据库连接.而每次创建数据库连接都需要访问,分配空闲资源,占用资源,释放资源,结束访问.非常的耗费时间和空间. 于是数据连接池技术便产生了,其原理就是在数据请求方和数据库之间建立一个数据库连接管理层.在系统启动的时候就分配一定数量的连接,并维护这些连接,保持最低数量的连接数.同时为了保护数据库和系统,设置一个最大连接数,缓解数据库的访问压力. 这样在每次

java Datasource,数据库连接池

目前有多重方式创建数据库连接池:JNDI,DBCP,C3P0等 为什么需要连接池: 使用java API DriverManager.getConnection()创建数据库连接,耗费内存和时间,实时性低:这种方式获取的connection需要手动关闭,不然会耗费大量的内存资源:对于频繁数据库操作,这种方式会造成性能低,尤其web应用 数据库连接池的功能: 负责创建.管理和分配数据库连接.初始化数据库连接池时,会创建一定数量的数据库连接对象,并存放于数据库连接池中.当请求连接数据库时,连接池会分

Java之中JDBC数据库连接池实现方法

作为一名初级Java程序员都很羡慕Windows ADO ,只需要new Connection 就可以直接从数据库连接池中返回Connection.并且 ADO Connection 是线程安全的,多个线程可以共用一个Connection,所以ASP程序一般都把getConnection 放在 Global.asa 文件中,在 IIS 启动时建立数据库连接.ADO 的Connection 和Result 都有很好的缓冲,并且很容易使用. 其实我们可以自己写一个JDBC数据库连接池. 写JDBC

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

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

Java语言链接数据库连接池配置的两种技巧

对于对性能要求较高的企业级应用来说用JDBC连接数据库的方式一般满足不了要求,这时就要用到数据库连接池了. 数据库连接池负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而再不是重新建立一个:释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏.这项技术能明显提高对数据库操作的性能. 配置数据库连接池的两种方法 Tomcat服务器配置步骤: 1.把下面这段代码粘贴D:\apache-tomcat-6.0.14\conf contex

Java -- JDBC 学习--数据库连接池

JDBC数据库连接池的必要性 在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: 在主程序(如servlet.beans)中建立数据库连接. 进行sql操作 断开数据库连接. 这种模式开发,存在的问题: 普通的JDBC数据库连接使用 DriverManager 来获取,每次向数据库建立连接的时候都要将 Connection 加载到内存中,再验证用户名和密码(得花费0.05s-1s的时间).需要数据库连接的时候,就向数据库要求一个,执行完成后再断开连接.这样的方式将会消耗大量的资源和

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

1.概述 频繁的创建和销毁数据库连接消耗非常多的系统资源,创建一个池子, 管理一定数量的连接,用的时候去池中取,用完了放回池中,这时比较通用的做法. 2.关键字 LinkedList  synchronized  InvocationHandler  CountDownLatch 3. 代码 3.1 ConnectionPool.java package com.rocky.pool; import java.sql.Connection; import java.util.LinkedList

JAVA写简单的数据库连接池

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

java之编写数据库连接池实现连接的复用

一般我们在操作数据库时候,需要频繁的打开和关闭连接,而创建数据库连接往往开销比较大,因而我们需要避免这种情况的发生,在这里我们可以创建一个连接池,当操作数据的时候,我们从连接池中取出连接,操作完毕后再将连接放回到池中. 在这里我们需要用到集合,大家知道ArrayList结合其实是一个数组,它读取数据的时候速度比较快,而LinkedList集合在操作的时候要比ArrayList要快的多,所以这里我们选择集合LinkedList. 1.编写一个连接池的类 1 package cn.mycast.ba