数据库连接池的学习

来源:互联网 发布:试析网络暴力 编辑:程序博客网 时间:2024/05/16 06:45

数据库连接池学习

数据库连接池的主要操作如下:  

 (1)建立数据库连接池对象(服务器启动)。  

 (2)按照事先指定的参数创建初始数量的数据库连接(即:空闲连接数)。  

 (3)对于一个数据库访问请求,直接从连接池中得到一个连接。如果数据库连接池对象中没有空闲的连接,且连接数没有达到最大(即:最大活跃连接数),创建一个新的数据库连接。  

 (4)存取数据库。   

  5)关闭数据库,释放所有数据库连接(此时的关闭数据库连接,并非真正关闭,而是将其放入空闲队列中。如实际空闲连接数大于初始空闲连接数则释放连接)。   

  6)释放数据库连接池对象(服务器停止、维护期间,释放数据库连接池对象,并释放所有连接)。

import java.util.HashMap;

import java.util.Vector;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.*;

public class ConnectionPool {

// 连接池的管理器,首先初始化,仅仅有一个对象,管理连接池

private static HashMap connectionPoolManager = new HashMap();

// 没有用过的连接池,用vector实现同步

private static Vector noUseConnectionPool;

// 没有用过的连接池

private static HashMap nowUseConnectionPool;

private static String dbDriver = "odbc:jdbc:OdbcJdbcDriver";

private static String dbUrl = "dalong@XX";

private static String userName = "dalong";

private static String userPassword = "dalong";

// 默认为100个连接池

private static int MAX_POOL = 100;

// singleTon 单例设计模式

private ConnectionPool(String driver, String url, String name,

String password, int max) throws ClassNotFoundException {

Class.forName(driver);

dbUrl = url;

userName = name;

userPassword = password;

MAX_POOL = max;

}

public static ConnectionPool getConnManagerInstance(String poolName)

throws ClassNotFoundException {

ConnectionPool tempPool = (ConnectionPool) connectionPoolManager//从map中取出连接池

.get(poolName);

if (tempPool == null) {

tempPool = new ConnectionPool(dbDriverdbUrluserName,

userPasswordMAX_POOL);

connectionPoolManager.put(poolName, tempPool);

return tempPool;

else

return tempPool;

}

// 通过连接池获得真正的链接

public static Connection getConnection() throws java.sql.SQLException {

Connection conn = null;

synchronized (noUseConnectionPool) {

if (noUseConnectionPool.size() > 0) {

conn = (Connection) noUseConnectionPool.firstElement();

noUseConnectionPool.remove(conn);

return conn;

}

}

// 如果数据库连接池没有链接了,自己创建一个

if (conn == null) {

conn = createConnection(dbDriverdbUrluserNameuserPassword);

else if (conn.isClosed()) {

nowUseConnectionPool.remove(conn);

conn = createConnection(dbDriverdbUrluserNameuserPassword);

}

conn.setAutoCommit(false);

nowUseConnectionPool.put(conn, conn);

return conn;

}

// 如果连接池没有链接了,就需要产生一个链接

private static Connection createConnection(String driver, String url,

String user, String password) throws java.sql.SQLException {

Connection conn = DriverManager.getConnection(url, user, password);

return conn;

}

public static void releaseConnection(Connection conn, boolean isCommit)

throws java.sql.SQLException {

if (isCommit)

conn.commit();

else

conn.rollback();

nowUseConnectionPool.remove(conn);

if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) {

synchronized (noUseConnectionPool) {

noUseConnectionPool.add(conn);

}

else {

conn.close();

}

}

public static void main(String[] args) {

// 测试模拟10个客户

for (int i = 0; i < 10; i++) {

try {

// xxxx 一般为属性文件读取

ConnectionPool pool = ConnectionPool

.getConnManagerInstance("xxxx");

Connection conn = pool.getConnection();

catch (SQLException ex1) {

// 处理异常

catch (ClassNotFoundException ex) {

// 处理异常

}

}

}

}