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

📄 connectionpool.java

📁 又一个课程设计 简易 JSP 论坛 功能较简单的那种, 界面上模仿了 Discuz JSP 本来就学的不行, 只是尽量实现了 MVC
💻 JAVA
字号:
package cn.ialvin.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;

public class ConnectionPool {
	public static void main(String[] a) throws SQLException, ClassNotFoundException {
		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection coxn = DriverManager.getConnection(
				"jdbc:odbc:Driver={SQL Server};Server=(local);Database=xbbs",
				"sa",
				"851225"
		);
		String sql = "SELECT TOP 1 [content] FROM [topic] WHERE [id]=6";
		PreparedStatement stmt = coxn.prepareStatement(sql);
		ResultSet rs = stmt.executeQuery();
		if (rs.next()) {
			System.out.println(rs.getString(1));
			System.out.println(3);
		}
		rs.close();
		stmt.close();
		coxn.close();
	}
	
	public synchronized static ConnectionPool newInstance(
			DBConfig config)
			throws SQLException, ClassNotFoundException {
		ConnectionPool pool = new ConnectionPool(config);
		return pool;
	}

	public synchronized void close() {
		Object o;
		synchronized (this.lock) {
			Iterator item = this.frees.iterator();
			while (item.hasNext()) {
				try {
					o = item.next();
					((Connection)o).close();
					System.out.println("关闭链接: " + o.toString());
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			item = this.busys.iterator();
			while (item.hasNext()) {
				try {
					o = item.next();
					((Connection)o).close();
					System.out.println("关闭链接: " + o.toString());
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			this.frees.clear();
			this.busys.clear();
			this.pool.clear();
			this.current = 0;
		}
	}
	
	public synchronized void finalize() throws Throwable {
		this.close();
		super.finalize();
	}
	
	synchronized Connection getConnection() throws PoolBusyException {
		Connection coxn = this.findConnection();
		for (int i=0; i<40 && coxn==null; i++) {
			try { Thread.sleep(50); } catch (InterruptedException e) {}
			coxn = this.findConnection();
		}
		if (coxn == null) {
			throw new PoolBusyException();
		}
		synchronized (this.lock) {
			this.busys.add(coxn);
		}
		//System.out.println("getConnection():" + coxn);
		return coxn;
	}

	synchronized void putConnection(Connection coxn) {
		//System.out.println("putConnection():" + coxn);
		synchronized (this.lock) {
			this.busys.remove(coxn);
			int i = this.getUseCount(coxn);
			if (i == 25) {
				try {
					if (!coxn.isClosed()) {
						coxn.close();
						System.out.println("关闭链接: " + coxn.toString());
						this.current--;
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			} else {
				this.frees.add(coxn);
			}
		}
	}

	private Object lock = new Object();
	private DBConfig config = null;
	private int max=0, min=0, current=0;
	private LinkedHashSet<Connection> busys = new LinkedHashSet<Connection>();
	private LinkedHashSet<Connection> frees = new LinkedHashSet<Connection>();
	private HashMap<Connection, Integer> pool = new HashMap<Connection, Integer>();
	public DBConfig getConfig() {
		return this.config;
	}
	private ConnectionPool(DBConfig config) 
			throws SQLException, ClassNotFoundException {
		if (config.getMax() < 1) throw new SQLException("最大连接数不能小于 1");
		if (config.getMin() > config.getMax()) throw new SQLException("最大连接数不能小于最小连接数");
		Class.forName(config.getDriver());
		Connection coxn = DriverManager.getConnection(config.getUrl(), config.getUID(), config.getPWD());
		coxn.close();
		this.config = config;
		this.max = config.getMax();
		this.min = config.getMin();
		for (int i=0; i<this.min; i++) {
			this.newConnection();
		}
	}

	private synchronized void newConnection() {
		if (this.current == this.max) return;
		Connection coxn = null;
		try {
			coxn = DriverManager.getConnection(this.config.getUrl(), this.config.getUID(), this.config.getPWD());
			System.out.println("创建连接:" + coxn.toString());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (coxn == null) return;
		synchronized (this.lock) {
			this.frees.add(coxn);
			this.pool.put(coxn, new Integer(0));
			this.current++;
		}
	}

	private int getUseCount(Connection coxn) {
		Integer i = (Integer)this.pool.get(coxn);
		return i.intValue();
	}
	private Connection findConnection() {
		Connection coxn = null;
		if (this.frees.isEmpty())
			this.newConnection();
		if (this.frees.isEmpty())
			return coxn;
		synchronized (this.lock) {
			Iterator item = this.frees.iterator();
			if (item.hasNext())
				coxn = (Connection)item.next();
			this.frees.remove(coxn);
			int i = this.getUseCount(coxn);
			pool.remove(coxn);
			pool.put(coxn, new Integer(i+1));
		}
		return coxn;
	}
}

⌨️ 快捷键说明

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