📄 poolmanager.java
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的 * * 这是一个数据库连接池操作类,支持多数据库操作, * 技术支持 => mailto: huliqing.cn@gmail.com */package biz.tbuy.common.pool;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import java.util.ArrayList;import java.util.Iterator;/** * @version 1.0 * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class PoolManager { private static PoolManager _poolManager = new PoolManager(); private Map<String, Pool> _pools = new HashMap<String, Pool>(); private Thread _poolListener; // 监测线程 private int _min = 5; // 连接池中最少的连接数 private int _max = 10; // 连接池中允许的最高连接数 private long _freeTime = 1000 * 60; // 空闲连接的超时时间 private long _busyTime = 1000 * 30; // 繁忙连接的超时时间 private long _timeout = 1000 * 60 * 60 * 4; // 连接的允许存在时间 private long _interval = 1000 * 5; // 线程的监测周期 private PoolManager() {} /** * @return PoolManager */ public static PoolManager getInstance() { return _poolManager; } /** * 设置空闲连接的时间,当空闲连接达到该时间,同时空闲连接数 * 大于连接池的最少限制数时,该连接将可能被释放 * @param freeTime - long */ public void setFreeTime(long freeTime) { _freeTime = freeTime; } /** * 获取空闲连接的时间限制 * @return freeTime */ public long getFreeTime() { return _freeTime; } /** * 设置繁忙连接的超时时间,默认为:30秒, * 当繁忙连接达到该时间时,该连接可能被强制释放 * @param busyTime - long */ public void setBusyTime(long busyTime) { _busyTime = busyTime; } /** * 获取繁忙连接的超时限制 * @return busyTime */ public long getBusyTime() { return _busyTime; } /** * 设置连接池的最少连接数 * @param min - int */ public void setMin(int min) { _min = min; } /** * 获取连接池的最小连接数限制 * @return int */ public int getMin() { return _min; } /** * 设置连接池的最大限制数,如果同时连接数达到该限制, * 那么其他请求可能进入等待状态. * @param max - int */ public void setMax(int max) { _max = max; } /** * 获取连接池中连接数的最大限制 * @return max */ public int getMax() { return _max; } /** * 根据给定的参数创建一个连接池 * @param id * @param driver * @param url * @param user * @param password */ public synchronized void createPool(String id, String driver, String url, String user, String password) { //System.out.println(id + ":" + driver + ":" + url + ":" + user + ":" + password); if (_pools.get(id) != null) { System.out.println("pool id=" + id + ", is already inside"); return; } Pool pool = new Pool(driver, url, user, password); _pools.put(id, pool); } /** * 启动线程监听连接的状态,同时控制连接及释放相 * 应的空闲连接 */ public void managerStart() { _poolListener = new Thread(new ManagerThread()); _poolListener.start(); } /** * 获取标识为id的连接池 * @param id * @return pool */ public Pool getPool(String id) { if (_pools != null) { return _pools.get(id); } return null; } /** * 显示所有连接池的状态信息 */ public void state() { if (_pools != null) { Set<String> keys = _pools.keySet(); for (String key : keys) { System.out.println("--" + key); _pools.get(key).state(); } } } /** * 获取连接池初始化过程中可能出现的错误! * @return errors; 包含错误信息的ArrayList */ public List<String> getErrors() { List<String> errors = new ArrayList<String>(2); if (_pools != null) { Set<String> keys = _pools.keySet(); for (String key : keys) { List<String> pErrors = _pools.get(key).getErrors(); for (String pError : pErrors) { errors.add(pError); } } } return errors; } /** start inner class ManagerThread **************/ public class ManagerThread implements Runnable{ public ManagerThread() {} public void run() { while (true) { if (!_pools.isEmpty()) { Set<String> keys = _pools.keySet(); for (String key : keys) { try { Pool pool = _pools.get(key); checkPool(pool); } catch (Exception e) {} } } // state(); try { Thread.sleep(_interval); } catch (InterruptedException ie) {} } } /** * 检测连接池状态,同时释放或关闭一些连接 */ private void checkPool(Pool pool) throws Exception{ List<ProxyConn> conns = pool.getConns(); Iterator<ProxyConn> iterator = conns.iterator(); while (iterator.hasNext()) { ProxyConn proxyConn = iterator.next(); // 如果该连接已经超过自己的允许生命时长,并且处于空闲中 if (proxyConn.getLiveTime() >= _timeout && !proxyConn.isBusy()) { proxyConn.realClose(); iterator.remove(); continue; } // 如果该连接已经无效,则关闭,同时移出池中 if (proxyConn.isClosed()) { proxyConn.realClose(); iterator.remove(); continue; } // 如果该连接已经达到繁忙时间限制,则强行释放之,使其空闲,但并不真正关闭 if (proxyConn.isBusy()) { if (proxyConn.getStateTime() >= _busyTime) { proxyConn.close(); } } else { // 如果该连接已经无所事事很久,且又是多余的,则释放、移除 if (proxyConn.getStateTime() >= _freeTime && conns.size() > _min) { proxyConn.realClose(); iterator.remove(); } } } // 整理连接池中的连接后,重新检查其中的连接数,如果数量少于最小限制数,则 // 创建新的连接 if (conns.size() < _min) { pool.checkAndCreate(); } } } /** end ManagerThread *****************************************************/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -