1 4 5 package org.smartlib.pool.core; 6 7 8 import java.util.*; 9 10 19 20 21 class ConnectionLeakPollThread implements Runnable { 22 23 private Vector connectionsInUse ; 24 private Vector connectionListenerList; 25 private String poolName; 26 private long sleepTime; 27 private boolean keepGoing ; 28 private long leakTimeOut ; 29 private static Debugger debug; 30 private Pool pool; 31 32 43 ConnectionLeakPollThread(Vector connectionsInUse 44 , Vector connectionListenerList , String poolName 45 , long sleepTime , long leakTimeOut , Pool pool) { 46 47 this.connectionsInUse = connectionsInUse; 48 this.connectionListenerList = connectionListenerList; 49 this.poolName = poolName; 50 this.sleepTime = sleepTime; 51 this.leakTimeOut = leakTimeOut; 52 keepGoing = true; 53 this.pool = pool; 54 debug = new Debugger("ConnectionLeakPollThread-" + poolName , 55 true ); 56 57 } 58 59 65 66 private void notifyAll(SmartConnection sConn) { 67 68 for (int i=0 ; i<connectionListenerList.size() ; i++ ) { 69 ConnectionLeakEvent cle = new ConnectionLeakEventImpl ( 71 sConn , sConn.getOwner() , sConn.getLastAccessedTime() , 72 sConn.getConnectionObtainedTime() , poolName); 73 ConnectionLeakListener c = 75 (ConnectionLeakListener)connectionListenerList.get(i); 76 c.connectionTimeOut(cle); 77 } 78 79 } 80 81 private void checkAndRelease() { 83 84 pool.releaseConnections(); 85 86 } 87 88 private void checkAndNotify() { 90 91 for (int i=0 ; i<connectionsInUse.size() ; i++ ) { 92 SmartConnection sConn = (SmartConnection)connectionsInUse.get(i); 93 if (System.currentTimeMillis() - sConn.getConnectionObtainedTime() 95 >= leakTimeOut) 96 notifyAll(sConn); 97 if (System.currentTimeMillis() - sConn.getLastAccessedTime() > ((PoolMonitor)pool).getConfigMonitor().getMaxConnectionIdleTime()) { 101 debug.print("Found a Idle Connection ..."); 102 sConn.forcedClose(); 103 } 104 } 105 106 } 107 108 public void run() { 111 debug.print("Starting Thread for detecting connection leaks"); 112 while (keepGoing) { 113 debug.print("Polling ............."); 114 checkAndNotify(); 115 checkAndRelease(); 116 try { 117 Thread.sleep(sleepTime); 118 } 119 catch (Exception e) { 120 } 121 } 122 123 } 124 125 private void stop() { 127 128 keepGoing = false; 129 130 } 131 132 } 133 | Popular Tags |