?? connectmanager.java
字號:
package mytools.sqlclient;import mytools.util.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2001</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class ConnectManager extends Thread{public static final int MAX_CONN_NUM=Config.getIntParmValue("mytools.max_connect_num"); //最大連接數public static final int INIT_CONN_NUM=Config.getIntParmValue("mytools.init_connect_num"); //初始ejb連接數public static final int MAX_IDLE_TIME=Config.getIntParmValue("mytools.max_idle_time"); //最大連接空閑時間(秒)public static final int MAX_IDLE_CONN_NUM=Config.getIntParmValue("mytools.max_idle_connect_num"); //最大空閑超時連接數public static final int INCREASE_CONN_NUM=Config.getIntParmValue("mytools.increase_connect_num"); //每次增加的連接數public static final int SCAN_INTERNAL=60; //檢查時間間隔(秒)private boolean isStop=false; //停止標志private int curConnectNum=0; //當前連接數private ConnectInfo[] conList=null; //連接信息private static Object lockObject = new Object();private static ConnectManager connectMng = null;private boolean isFirstSql = true ;//是否用第一個sql服務器端口號 protected ConnectManager() { init(); this.start(); } /** 功能:管理連接處理 @param 無 @return 無 */ public void run() { while(true) { Util.sleep(SCAN_INTERNAL); synchronized(lockObject) { if (isStop) break; int li_idle_num=getTimeOutIdleConnectNum(); //超時空閑連接數 if (li_idle_num>MAX_IDLE_CONN_NUM) { int li_remove_num=li_idle_num-MAX_IDLE_CONN_NUM; removeIdleConnects(li_remove_num); } //根據需要分配連接 if (hasAvailabelResource()==null) //無可用連接了,需增加連接 { //分配連接 if (curConnectNum<MAX_CONN_NUM) { addConnects(); //增加一些連接 lockObject.notifyAll(); } } } }//end of while } /** 功能:刪除指定數目的空閑連接 @param pi_remove_num 刪除數 @return 無 */ private void removeIdleConnects(int pi_remove_num) { int li_remove_num=0; for (int i=0;i<curConnectNum;i++) { if (isTimeOutIdleConnect(conList[i])) { removeConnect(i); li_remove_num++; if (li_remove_num>=pi_remove_num) break; } } curConnectNum-=li_remove_num; } /** 功能:刪除指定的連接 @param pi_idx 連接索引 @return 無 */ private void removeConnect(int pi_idx) { try { Util.logInfo("remove connect:"+conList[pi_idx].getConnection()); conList[pi_idx].getConnection().close(); } catch(Exception ex) { ex.printStackTrace(); Util.error("removeConnect:exception:"+ex); } for (int i=pi_idx+1;i<curConnectNum;i++) { conList[i-1]=conList[i]; } } /** 功能:判斷指定連接是否為超時空閑連接 @param po_info 連接信息 @return true 是 false 非 */ private boolean isTimeOutIdleConnect(ConnectInfo po_info) { long ll_idle_time=System.currentTimeMillis()-po_info.getBeginTime(); if (!po_info.isUsed() && (ll_idle_time>MAX_IDLE_TIME*1000)) return true; return false; } /** 功能:取得超時空閑連接數 @param 無 @return 超時空閑連接數 */ private int getTimeOutIdleConnectNum() { int li_idle_num=0; for (int i=0;i<curConnectNum;i++) { if (isTimeOutIdleConnect(conList[i])) { li_idle_num++; } } return li_idle_num; } /** 功能:增加一些起連接數 @param 無 @return 無 */ private void addConnects() { int li_remain_num=MAX_CONN_NUM-curConnectNum; int li_add_num=INCREASE_CONN_NUM>li_remain_num?li_remain_num:INCREASE_CONN_NUM; for (int i=0;i<li_add_num;i++) { addConnect(); } } /** 功能:增加一個連接 @param 無 @return 無 */ private void addConnect() { IConnection lo_conn=createNewConnection(); if (lo_conn==null) { Util.error("addConnect:建立連接失敗!"); return ; } Util.logInfo("create new connection:"+lo_conn); conList[curConnectNum++]=new ConnectInfo("",lo_conn,false,false); } /** 功能:取得一個實例 @param 無 @return 一個實例 */ public static ConnectManager getInstance() { if (connectMng == null) { synchronized (lockObject) { if (connectMng == null) { connectMng = new ConnectManager(); } } } return connectMng; } /** 功能:初始化連接信息 @param 無 @return true 成功 false 失敗 */ private synchronized boolean init() { conList=new ConnectInfo[MAX_CONN_NUM]; //初始化建立INIT_CONN_NUM個連接 for (int i=0;i<INIT_CONN_NUM;i++) { addConnect(); } Util.logInfo("初始化("+curConnectNum+")個連接成功 !"); return true; } /** 功能: 取得當前線程名 @param 無 @return 當前線程名 */ private String getCurThreadName() { return Thread.currentThread().getName(); } /** 功能:查找當前線程的連接 @param ps_threadName 線程名 @return 成功 數據庫連接對象 失敗或找不到 null */ private IConnection findConnect(String ps_threadName) { int i; for (i=0;i<curConnectNum;i++) { if (conList[i].getThreadName().equals(ps_threadName)&& conList[i].isUsed()) { return conList[i].getConnection(); } } return null; } /** 功能:創建一個數據庫連接 @param 無 @return 成功 數據庫連接 失敗 null */ private synchronized IConnection createNewConnection() { return ConnectionFactory.createNewConnection(); } /** 功能:釋放當前線程的指定的連接 @param 無 @return 無 */ public void releaseConnection() { //Util.logInfo("in releaseConnection:"+getCurThreadName()); synchronized(lockObject) { String ls_threadName=getCurThreadName(); for (int i=0;i<curConnectNum;i++) { //該連接不存在未提交的事務,才釋放 if ((!conList[i].isInTransaction()) && conList[i].getThreadName().equals(ls_threadName)) { //Util.logInfo("now releaseConnection:"+ls_threadName); conList[i].notUse(); //不再占用 lockObject.notifyAll(); break; } } } //Util.logInfo("end of releaseConnection:"+getCurThreadName()); } /** 功能:設置當前線程的連接正在事務中 @param 無 @return 無 */ public void setInTransaction() { setTranStatus(true); //Util.logInfo("end of setInTransaction :"+ls_threadName); } /** 功能:設置連接未在事務狀態中 @param 無 @return 無 */ public void setNotInTransaction() { setTranStatus(false); } /** 功能:設置連接的事務狀態 @param ps_status 狀態 @return 無 */ private void setTranStatus(boolean pb_status) { String ls_threadName=getCurThreadName(); //Util.logInfo("setInTransaction :"+ls_threadName); synchronized(lockObject) { for (int i=0;i<curConnectNum;i++) { if (conList[i].getThreadName().equals(ls_threadName)) { conList[i].setInTransaction(pb_status); //設置在事務中標志 return ; } } } } /** 功能: 查找一個數據庫連接 @param 無 @return 成功 數據庫連接 失敗 null */ public IConnection getConnection() { String ls_threadName; while (true) { synchronized (lockObject) { ls_threadName=getCurThreadName(); //當前線程名 //找一個現有的連接 IConnection lo_conn=findConnect(ls_threadName); if (lo_conn!=null) return lo_conn; //找一個可用連接 ConnectInfo lo_info=hasAvailabelResource(); if (lo_info!=null) { lo_info.useBy(ls_threadName); //占用本連接 return lo_info.getConnection(); } Util.logInfo("wait connect ! threadName:"+ls_threadName); //找不到,只好等了 try { lockObject.wait(); }catch(Exception ex) { ex.printStackTrace(); } } } } /** 功能:判斷是否有可用的連接 @param 無 @return 有 連接對象 無 null */ private ConnectInfo hasAvailabelResource() { for (int i=0;i<curConnectNum;i++) { if (!conList[i].isUsed()) return conList[i]; } return null; } /** 功能:取得當前連接數 @param 無 @return 當前連接數 */ public int getConnectionCount() { return this.curConnectNum; } /** 功能:關閉所有連接 @param 無 @return 無 */ public void closeAllConnection() { if (conList==null) return ; for (int i=0;i<curConnectNum;i++) { IConnection lo_conn=conList[i].getConnection(); if (lo_conn!=null) { try { lo_conn.close(); lo_conn=null; }catch(Exception ex){} conList[i].reset(); } } } /** 功能: 顯示連接信息 @param 無 @return 無 */ public void displayConnectInfo() { if (conList==null) { Util.logInfo("displayConnectInfo:no connections"); return ; } for (int i=0;i<curConnectNum;i++) { System.out.println(conList[i]); } } /** 功能:停止調度,關閉所有連接 @param 無 @return 無 */ public void shutdown() { synchronized(lockObject) { isStop=true; } closeAllConnection(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -