码迷,mamicode.com
首页 > 数据库 > 详细

JDBC -- Connection Pool

时间:2017-09-06 14:30:26      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:syn   exce   into   for   stat   jdb   nec   size   and   

Connection Pool: create many connection objects in advance, and put the connection into the cache(list). The client will get the connection from the cache, return the connectio to the cache after using it. This way could improve the access effeciency of data base.

Simulate the connection pool:

package com.pp.pool;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.pp.util.JdbcUtil;

/*
 * Simulate the completion of connetion pool
 */
public class SimpleConnectionPool {
    private static List<Connection> pool = new ArrayList<Connection>();

    static {
        for (int i = 0; i < 10; i++) {
            Connection conn = JdbcUtil.getConnection();
            pool.add(conn);
        }

    }

    // get the connection object from cache pool
    public synchronized static Connection getConnection() {
        if (pool.size() > 0) {
            Connection conn = pool.remove(0);
            return conn;
        } else {
            throw new RuntimeException("Server is busy!");
        }
    }

    // return the connection
    public static void releas(Connection conn) {
        pool.add(conn);
    }
}

 

JDBC -- Connection Pool

标签:syn   exce   into   for   stat   jdb   nec   size   and   

原文地址:http://www.cnblogs.com/ppcoder/p/7484301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!