1 16 17 package org.apache.commons.dbcp.datasources; 18 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.sql.Connection ; 22 import java.sql.SQLException ; 23 import java.util.Map ; 24 25 import javax.naming.NamingException ; 26 import javax.sql.ConnectionPoolDataSource ; 27 28 import org.apache.commons.collections.LRUMap; 29 import org.apache.commons.pool.KeyedObjectPool; 30 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 31 import org.apache.commons.pool.impl.GenericObjectPool; 32 import org.apache.commons.dbcp.SQLNestedException; 33 34 43 public class SharedPoolDataSource 44 extends InstanceKeyDataSource { 45 46 private static final Map userKeys = new LRUMap(10); 47 48 private int maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE; 49 private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE; 50 private int maxWait = (int)Math.min((long)Integer.MAX_VALUE, 51 GenericObjectPool.DEFAULT_MAX_WAIT); 52 private KeyedObjectPool pool = null; 53 54 57 public SharedPoolDataSource() { 58 } 59 60 63 public void close() throws Exception { 64 pool.close(); 65 InstanceKeyObjectFactory.removeInstance(instanceKey); 66 } 67 68 71 76 public int getMaxActive() { 77 return (this.maxActive); 78 } 79 80 85 public void setMaxActive(int maxActive) { 86 assertInitializationAllowed(); 87 this.maxActive = maxActive; 88 } 89 90 95 public int getMaxIdle() { 96 return (this.maxIdle); 97 } 98 99 104 public void setMaxIdle(int maxIdle) { 105 assertInitializationAllowed(); 106 this.maxIdle = maxIdle; 107 } 108 109 116 public int getMaxWait() { 117 return (this.maxWait); 118 } 119 120 127 public void setMaxWait(int maxWait) { 128 assertInitializationAllowed(); 129 this.maxWait = maxWait; 130 } 131 132 135 138 public int getNumActive() { 139 return (pool == null) ? 0 : pool.getNumActive(); 140 } 141 142 145 public int getNumIdle() { 146 return (pool == null) ? 0 : pool.getNumIdle(); 147 } 148 149 152 protected synchronized PooledConnectionAndInfo 153 getPooledConnectionAndInfo(String username, String password) 154 throws SQLException { 155 if (pool == null) { 156 try { 157 registerPool(username, password); 158 } catch (NamingException e) { 159 throw new SQLNestedException("RegisterPool failed", e); 160 } 161 } 162 163 PooledConnectionAndInfo info = null; 164 try { 165 info = (PooledConnectionAndInfo) pool 166 .borrowObject(getUserPassKey(username, password)); 167 } 168 catch (Exception e) { 169 throw new SQLNestedException( 170 "Could not retrieve connection info from pool", e); 171 } 172 return info; 173 } 174 175 private UserPassKey getUserPassKey(String username, String password) { 176 UserPassKey key = (UserPassKey) userKeys.get(username); 177 if (key == null) { 178 key = new UserPassKey(username, password); 179 userKeys.put(username, key); 180 } 181 return key; 182 } 183 184 private void registerPool( 185 String username, String password) 186 throws javax.naming.NamingException , SQLException { 187 188 ConnectionPoolDataSource cpds = testCPDS(username, password); 189 190 GenericKeyedObjectPool tmpPool = new GenericKeyedObjectPool(null); 192 tmpPool.setMaxActive(getMaxActive()); 193 tmpPool.setMaxIdle(getMaxIdle()); 194 tmpPool.setMaxWait(getMaxWait()); 195 tmpPool.setWhenExhaustedAction(whenExhaustedAction(maxActive, maxWait)); 196 tmpPool.setTestOnBorrow(getTestOnBorrow()); 197 tmpPool.setTestOnReturn(getTestOnReturn()); 198 tmpPool.setTimeBetweenEvictionRunsMillis( 199 getTimeBetweenEvictionRunsMillis()); 200 tmpPool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun()); 201 tmpPool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); 202 tmpPool.setTestWhileIdle(getTestWhileIdle()); 203 pool = tmpPool; 204 new KeyedCPDSConnectionFactory(cpds, pool, getValidationQuery()); 208 } 209 210 protected void setupDefaults(Connection con, String username) 211 throws SQLException { 212 con.setAutoCommit(isDefaultAutoCommit()); 213 con.setReadOnly(isDefaultReadOnly()); 214 int defaultTransactionIsolation = getDefaultTransactionIsolation(); 215 if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION) { 216 con.setTransactionIsolation(defaultTransactionIsolation); 217 } 218 } 219 220 227 private void readObject(ObjectInputStream in) 228 throws IOException , ClassNotFoundException { 229 try 230 { 231 in.defaultReadObject(); 232 SharedPoolDataSource oldDS = (SharedPoolDataSource) 233 new SharedPoolDataSourceFactory() 234 .getObjectInstance(getReference(), null, null, null); 235 this.pool = oldDS.pool; 236 } 237 catch (NamingException e) 238 { 239 throw new IOException ("NamingException: " + e); 240 } 241 } 242 } 243 244 | Popular Tags |