Your browser does not support JavaScript and this site utilizes JavaScript to build content and provide links to additional information. You should either enable JavaScript in your browser settings or use a browser that supports JavaScript in order to take full advantage of this site.
1 16 17 package org.apache.commons.dbcp; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.pool.PoolableObjectFactory; 24 import org.apache.commons.pool.impl.GenericObjectPool; 25 26 36 public class AbandonedObjectPool extends GenericObjectPool { 37 38 private AbandonedConfig config = null; 40 private List trace = new ArrayList (); 42 43 49 public AbandonedObjectPool(PoolableObjectFactory factory, 50 AbandonedConfig config) { 51 super(factory); 52 this.config = config; 53 System.out.println("AbandonedObjectPool is used (" + this + ")"); 54 System.out.println(" LogAbandoned: " + config.getLogAbandoned()); 55 System.out.println(" RemoveAbandoned: " + config.getRemoveAbandoned()); 56 System.out.println(" RemoveAbandonedTimeout: " + config.getRemoveAbandonedTimeout()); 57 } 58 59 67 public Object borrowObject() throws Exception { 68 if (config != null 69 && config.getRemoveAbandoned() 70 && (getNumIdle() < 2) 71 && (getNumActive() > getMaxActive() - 3) ) { 72 removeAbandoned(); 73 } 74 Object obj = super.borrowObject(); 75 if(obj instanceof AbandonedTrace) { 76 ((AbandonedTrace)obj).setStackTrace(); 77 } 78 if (obj != null && config != null && config.getRemoveAbandoned()) { 79 synchronized(trace) { 80 trace.add(obj); 81 } 82 } 83 return obj; 84 } 85 86 91 public void returnObject(Object obj) throws Exception { 92 if (config != null && config.getRemoveAbandoned()) { 93 synchronized(trace) { 94 boolean foundObject = trace.remove(obj); 95 if (!foundObject) { 96 return; } 98 } 99 } 100 super.returnObject(obj); 101 } 102 103 public void invalidateObject(Object obj) throws Exception { 104 if (config != null && config.getRemoveAbandoned()) { 105 synchronized(trace) { 106 boolean foundObject = trace.remove(obj); 107 if (!foundObject) { 108 return; } 110 } 111 } 112 super.invalidateObject(obj); 113 } 114 115 119 private void removeAbandoned() { 120 long now = System.currentTimeMillis(); 122 long timeout = now - (config.getRemoveAbandonedTimeout() * 1000); 123 ArrayList remove = new ArrayList (); 124 synchronized(trace) { 125 Iterator it = trace.iterator(); 126 while (it.hasNext()) { 127 AbandonedTrace pc = (AbandonedTrace)it.next(); 128 if (pc.getLastUsed() > timeout) { 129 continue; 130 } 131 if (pc.getLastUsed() > 0) { 132 remove.add(pc); 133 } 134 } 135 } 136 137 Iterator it = remove.iterator(); 139 while (it.hasNext()) { 140 AbandonedTrace pc = (AbandonedTrace)it.next(); 141 if (config.getLogAbandoned()) { 142 pc.printStackTrace(); 143 } 144 try { 145 invalidateObject(pc); 146 } catch(Exception e) { 147 e.printStackTrace(); 148 } 149 } 150 } 151 } 152
| Popular Tags
|