JDBC -- Connection Pool

Posted pp_crz_coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC -- Connection Pool相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

如何监控数据库连接池(JDBC Connection Pool)

尽管用户存在且凭据正常,但 Ping-Connection-Pool 向 DB 抛出拒绝访问

JDBC自定义连接池

Java Connection Pool 和 try-with 语句:连接实际上是关闭还是返回到池中?

JDBC 工具类

org.apache.tomcat.jdbc.pool 中的验证查询