1 6 7 package org.jfox.jdbc.datasource; 8 9 import java.sql.Connection ; 10 import java.sql.SQLException ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import org.jfox.ioc.common.AbstractComponent; 16 import org.jfox.pool.AbstractObjectPool; 17 import org.jfox.pool.ObjectFactory; 18 import org.jfox.pool.ObjectPool; 19 import org.jfox.pool.PoolableObject; 20 import org.jfox.pool.SimpleObjectPool; 21 22 25 26 public class ConnectionPool extends AbstractComponent implements ObjectPool { 27 28 private AbstractObjectPool pool = null; 29 private ConnectionFactory factory = null; 30 31 private int initNum = 2; 32 private int maxRest = 10; 33 34 private Map workings = new HashMap (); 36 37 private long timeout = 60 * 60 * 1000L; 39 public ConnectionPool(String dbDriver, String dbURL, String user, String password) throws Exception { 40 factory = new ConnectionFactory(PoolableConnection.class, dbDriver, dbURL, user, password); 41 } 42 43 public Connection getConnection() throws Exception { 44 PoolableConnection pconn = (PoolableConnection) retrieveObject(); 45 return pconn; 46 } 47 48 public int getInitNum() { 49 return initNum; 50 } 51 52 public void setInitNum(int initNum) { 53 this.initNum = initNum; 54 } 55 56 public void clear() { 57 try { 58 for(Iterator it = workings.keySet().iterator(); it.hasNext();) { 59 ((PoolableConnection) it.next()).getConnection().close(); 60 } 61 } 62 catch(SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 67 public int getMaxRest() { 68 return maxRest; 69 } 70 71 public void setMaxRest(int maxRest) { 72 this.maxRest = maxRest; 73 } 74 75 public ObjectFactory getObjectFactory() { 76 return factory; 77 } 78 79 public boolean removeObject(PoolableObject obj) { 80 workings.remove(obj); 81 try { 82 factory.destroyObject(obj); 83 return true; 84 } 85 catch(Exception e) { 86 e.printStackTrace(); 87 return false; 88 } 89 } 90 91 public int getWorking() { 92 return workings.size(); 93 } 94 95 public String getObjectClass() { 96 return factory.getObjectClass().getName(); 97 } 98 99 public int getRest() { 100 return pool.getRest(); 101 } 102 103 public PoolableObject retrieveObject() throws Exception { 104 PoolableConnection pobj = (PoolableConnection) pool.retrieveObject(); 105 pobj.setPool(this); 106 workings.put(pobj, new Long (System.currentTimeMillis())); 107 return pobj; 108 } 109 110 public boolean restoreObject(PoolableObject obj) { 111 workings.remove(obj); 112 return pool.restoreObject(obj); 113 } 114 115 protected void doInit() throws Exception { 116 pool = new SimpleObjectPool(factory, initNum, maxRest); 117 pool.init(); 118 new LifecyleThread().start(); 119 } 120 121 protected void doDestroy() throws Exception { 122 clear(); 123 pool.destroy(); 124 } 125 126 129 private class LifecyleThread extends Thread { 130 public LifecyleThread() { 131 this.setPriority(Thread.MIN_PRIORITY); 132 this.setDaemon(true); 133 } 134 135 public void run() { 136 while(true) { 137 try { 138 Thread.sleep(timeout / 10); 139 } 140 catch(Exception e) { 141 } 142 for(Iterator it = workings.entrySet().iterator(); it.hasNext();) { 143 Map.Entry entry = (Map.Entry ) it.next(); 144 long time = ((Long ) entry.getValue()).longValue(); 145 if(System.currentTimeMillis() - time > timeout) { 146 try { 147 factory.destroyObject((PoolableObject) entry.getKey()); 148 } 149 catch(Exception e) { 150 e.printStackTrace(); 151 } 152 } 153 } 154 } 155 } 156 } 157 158 public static void main(String [] args) { 159 160 } 161 } | Popular Tags |