⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionpool.java

📁 本方案是在与***信息技术(北京)有限公司相关人员协商并分析了北京****投资有限公司综合信息管理系统(以下简称“**管理系统”)相关资料之后提交的网络安全解决方案。 本方案描述***管理系统的网络
💻 JAVA
字号:
package com.test.dao.pool;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class ConnectionPool {
	private List<_Connection> connections;
	private ConnectionPoolConfig poolConfig;
	private static ConnectionPool connectionPool = null;
	
	public static ConnectionPool getConnectionPool() throws SQLException{
		if(connectionPool==null){
			connectionPool = new ConnectionPool( new ConnectionPoolConfig() );
		}
		return connectionPool;
	}
	
	public synchronized ConnectionPool reDeploy(ConnectionPoolConfig poolConfig) throws SQLException{
		this.clear();
		this.init(poolConfig);
		return connectionPool;
	}
	
	private ConnectionPool(ConnectionPoolConfig poolConfig) throws SQLException {
		this.init(poolConfig);
	}

	private void init(ConnectionPoolConfig poolConfig) throws SQLException {
		if (poolConfig == null) {
			throw new SQLException("ConnectionPool:请传入正确的poolConfig");
		}
		this.poolConfig = poolConfig;
		this.loadDriver();
		this.connections = new Vector<_Connection>(this.poolConfig
				.getMinConnection() + 5);
		if (this.getConnectionCount() < this.poolConfig.getMinConnection()) {
			try {
				for (int i = 0; i < this.poolConfig.getMinConnection(); i++) {
					this.connections.add(this.createNewConnection());
				}
			} catch (SQLException e) {
				e.printStackTrace();
				if (this.getConnectionCount() == 0) {
					throw new SQLException("ConnectionPool数据库连接创建错误,检查配置文件");
				}
			}
		}
	}

	private void loadDriver() {
		try {
			Class.forName(poolConfig.getDriver());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("ConnectionPool:无法加载数据库驱动类,请等待管理员修复");
		}
	}

	public synchronized Connection getConnection() throws SQLException {
		_Connection _conn = null;
		_conn = this.getFreeConnection(this.poolConfig.getWaitTime());
		if (_conn == null) {
			try {
				if (this.getConnectionCount() < this.poolConfig.getMaxConnection()) {
					_conn = this.createNewConnection();
				} else {
					throw new SQLException();
				}
			} catch (SQLException e) {
				throw new SQLException("ConnectionPool:暂时没有可供使用的数据库连接,请稍候重试");
			}
		}
		_conn.setWorking(true);
		_conn.setReadOnly(false);
		_conn.setAutoCommit(true);
		this.autoClose();
		return _conn;
	}

	public synchronized void closeConnection(Connection conn) {
		if(conn instanceof _Connection){
			((_Connection)conn).destroy();
			this.connections.remove(conn);
		}else{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	private synchronized void clear() {
		for (int i = 0; i < this.getConnectionCount(); i++) {
			_Connection conn = this.connections.get(i);
			conn.destroy();
			this.connections.remove(i);
		}
	}

	public synchronized int getConnectionCount() {
		return this.connections.size();
	}

	private void autoClose() {
		Iterator<_Connection> iter = this.connections.iterator();
		while (iter.hasNext()) {
			if (this.getConnectionCount() <= this.poolConfig.getMinConnection()) {
				return;
			}
			_Connection _conn = iter.next();
			if ((_conn.getLastAccessTime() + this.poolConfig.getTimeoutValue()) < System
					.currentTimeMillis()) {
				this.closeConnection(_conn);
			}
		}
	}

	private _Connection createNewConnection() throws SQLException {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(poolConfig.getUrl(), poolConfig
					.getUser(), poolConfig.getPassword());
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
		_Connection _con = new _Connection(conn, false);
		this.connections.add(_con);
		return _con;
	}

	private _Connection getFreeConnection(long timeout) {
		_Connection _conn = null;
		for (int i = 0; i < this.getConnectionCount(); i++) {
			_conn = connections.get(i);
			if (!_conn.isWorking()) {
				return _conn;
			}
		}

		if (timeout > 0) {
			try {
				this.wait(timeout);
			} catch (InterruptedException e) {
				e.printStackTrace();
				return null;
			}
			_conn = this.getFreeConnection(0);
		}
		return _conn;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -