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.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 import javax.naming.NamingException ; 28 import javax.sql.ConnectionPoolDataSource ; 29 30 import org.apache.commons.pool.ObjectPool; 31 import org.apache.commons.pool.impl.GenericObjectPool; 32 import org.apache.commons.dbcp.SQLNestedException; 33 34 49 public class PerUserPoolDataSource 50 extends InstanceKeyDataSource { 51 private static final Map poolKeys = new HashMap (); 52 53 private int defaultMaxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE; 54 private int defaultMaxIdle = GenericObjectPool.DEFAULT_MAX_IDLE; 55 private int defaultMaxWait = (int)Math.min((long)Integer.MAX_VALUE, 56 GenericObjectPool.DEFAULT_MAX_WAIT); 57 Map perUserDefaultAutoCommit = null; 58 Map perUserDefaultTransactionIsolation = null; 59 Map perUserMaxActive = null; 60 Map perUserMaxIdle = null; 61 Map perUserMaxWait = null; 62 Map perUserDefaultReadOnly = null; 63 64 private transient Map pools = new HashMap (); 65 66 69 public PerUserPoolDataSource() { 70 } 71 72 75 private static void close(Map poolMap) { 76 } 77 78 81 public void close() { 82 for (Iterator poolIter = pools.values().iterator(); 83 poolIter.hasNext();) { 84 try { 85 ((ObjectPool) poolIter.next()).close(); 86 } catch (Exception closePoolException) { 87 } 89 } 90 InstanceKeyObjectFactory.removeInstance(instanceKey); 91 } 92 93 96 102 public int getDefaultMaxActive() { 103 return (this.defaultMaxActive); 104 } 105 106 112 public void setDefaultMaxActive(int maxActive) { 113 assertInitializationAllowed(); 114 this.defaultMaxActive = maxActive; 115 } 116 117 123 public int getDefaultMaxIdle() { 124 return (this.defaultMaxIdle); 125 } 126 127 133 public void setDefaultMaxIdle(int defaultMaxIdle) { 134 assertInitializationAllowed(); 135 this.defaultMaxIdle = defaultMaxIdle; 136 } 137 138 146 public int getDefaultMaxWait() { 147 return (this.defaultMaxWait); 148 } 149 150 158 public void setDefaultMaxWait(int defaultMaxWait) { 159 assertInitializationAllowed(); 160 this.defaultMaxWait = defaultMaxWait; 161 } 162 163 167 public Boolean getPerUserDefaultAutoCommit(String key) { 168 Boolean value = null; 169 if (perUserDefaultAutoCommit != null) { 170 value = (Boolean ) perUserDefaultAutoCommit.get(key); 171 } 172 return value; 173 } 174 175 179 public void setPerUserDefaultAutoCommit(String username, Boolean value) { 180 assertInitializationAllowed(); 181 if (perUserDefaultAutoCommit == null) { 182 perUserDefaultAutoCommit = new HashMap (); 183 } 184 perUserDefaultAutoCommit.put(username, value); 185 } 186 187 191 public Integer getPerUserDefaultTransactionIsolation(String username) { 192 Integer value = null; 193 if (perUserDefaultTransactionIsolation != null) { 194 value = (Integer ) perUserDefaultTransactionIsolation.get(username); 195 } 196 return value; 197 } 198 199 203 public void setPerUserDefaultTransactionIsolation(String username, 204 Integer value) { 205 assertInitializationAllowed(); 206 if (perUserDefaultTransactionIsolation == null) { 207 perUserDefaultTransactionIsolation = new HashMap (); 208 } 209 perUserDefaultTransactionIsolation.put(username, value); 210 } 211 212 218 public Integer getPerUserMaxActive(String username) { 219 Integer value = null; 220 if (perUserMaxActive != null) { 221 value = (Integer ) perUserMaxActive.get(username); 222 } 223 return value; 224 } 225 226 232 public void setPerUserMaxActive(String username, Integer value) { 233 assertInitializationAllowed(); 234 if (perUserMaxActive == null) { 235 perUserMaxActive = new HashMap (); 236 } 237 perUserMaxActive.put(username, value); 238 } 239 240 241 247 public Integer getPerUserMaxIdle(String username) { 248 Integer value = null; 249 if (perUserMaxIdle != null) { 250 value = (Integer ) perUserMaxIdle.get(username); 251 } 252 return value; 253 } 254 255 261 public void setPerUserMaxIdle(String username, Integer value) { 262 assertInitializationAllowed(); 263 if (perUserMaxIdle == null) { 264 perUserMaxIdle = new HashMap (); 265 } 266 perUserMaxIdle.put(username, value); 267 } 268 269 277 public Integer getPerUserMaxWait(String username) { 278 Integer value = null; 279 if (perUserMaxWait != null) { 280 value = (Integer ) perUserMaxWait.get(username); 281 } 282 return value; 283 } 284 285 293 public void setPerUserMaxWait(String username, Integer value) { 294 assertInitializationAllowed(); 295 if (perUserMaxWait == null) { 296 perUserMaxWait = new HashMap (); 297 } 298 perUserMaxWait.put(username, value); 299 } 300 301 305 public Boolean getPerUserDefaultReadOnly(String username) { 306 Boolean value = null; 307 if (perUserDefaultReadOnly != null) { 308 value = (Boolean ) perUserDefaultReadOnly.get(username); 309 } 310 return value; 311 } 312 313 317 public void setPerUserDefaultReadOnly(String username, Boolean value) { 318 assertInitializationAllowed(); 319 if (perUserDefaultReadOnly == null) { 320 perUserDefaultReadOnly = new HashMap (); 321 } 322 perUserDefaultReadOnly.put(username, value); 323 } 324 325 328 331 public int getNumActive() { 332 return getNumActive(null, null); 333 } 334 335 338 public int getNumActive(String username, String password) { 339 ObjectPool pool = (ObjectPool)pools.get(getPoolKey(username)); 340 return (pool == null) ? 0 : pool.getNumActive(); 341 } 342 343 346 public int getNumIdle() { 347 return getNumIdle(null, null); 348 } 349 350 353 public int getNumIdle(String username, String password) { 354 ObjectPool pool = (ObjectPool)pools.get(getPoolKey(username)); 355 return (pool == null) ? 0 : pool.getNumIdle(); 356 } 357 358 359 362 protected synchronized PooledConnectionAndInfo 363 getPooledConnectionAndInfo(String username, String password) 364 throws SQLException { 365 366 PoolKey key = getPoolKey(username); 367 Object pool = pools.get(key); 368 if (pool == null) { 369 try { 370 registerPool(username, password); 371 pool = pools.get(key); 372 } catch (NamingException e) { 373 throw new SQLNestedException("RegisterPool failed", e); 374 } 375 } 376 377 PooledConnectionAndInfo info = null; 378 try { 379 info = (PooledConnectionAndInfo)((ObjectPool) pool).borrowObject(); 380 } 381 catch (Exception e) { 382 throw new SQLNestedException( 383 "Could not retrieve connection info from pool", e); 384 } 385 386 return info; 387 } 388 389 protected void setupDefaults(Connection con, String username) 390 throws SQLException { 391 boolean defaultAutoCommit = isDefaultAutoCommit(); 392 if (username != null) { 393 Boolean userMax = getPerUserDefaultAutoCommit(username); 394 if (userMax != null) { 395 defaultAutoCommit = userMax.booleanValue(); 396 } 397 } 398 399 boolean defaultReadOnly = isDefaultReadOnly(); 400 if (username != null) { 401 Boolean userMax = getPerUserDefaultReadOnly(username); 402 if (userMax != null) { 403 defaultReadOnly = userMax.booleanValue(); 404 } 405 } 406 407 int defaultTransactionIsolation = getDefaultTransactionIsolation(); 408 if (username != null) { 409 Integer userMax = getPerUserDefaultTransactionIsolation(username); 410 if (userMax != null) { 411 defaultTransactionIsolation = userMax.intValue(); 412 } 413 } 414 415 con.setAutoCommit(defaultAutoCommit); 416 con.setReadOnly(defaultReadOnly); 417 if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION) { 418 con.setTransactionIsolation(defaultTransactionIsolation); 419 } 420 } 421 422 private PoolKey getPoolKey(String username) { 423 PoolKey key = null; 424 String dsName = getDataSourceName(); 425 Map dsMap = (Map ) poolKeys.get(dsName); 426 if (dsMap != null) { 427 key = (PoolKey) dsMap.get(username); 428 } 429 430 if (key == null) { 431 key = new PoolKey(dsName, username); 432 if (dsMap == null) { 433 dsMap = new HashMap (); 434 poolKeys.put(dsName, dsMap); 435 } 436 dsMap.put(username, key); 437 } 438 return key; 439 } 440 441 private synchronized void registerPool( 442 String username, String password) 443 throws javax.naming.NamingException , SQLException { 444 445 ConnectionPoolDataSource cpds = testCPDS(username, password); 446 447 Integer userMax = getPerUserMaxActive(username); 448 int maxActive = (userMax == null) ? 449 getDefaultMaxActive() : userMax.intValue(); 450 userMax = getPerUserMaxIdle(username); 451 int maxIdle = (userMax == null) ? 452 getDefaultMaxIdle() : userMax.intValue(); 453 userMax = getPerUserMaxWait(username); 454 int maxWait = (userMax == null) ? 455 getDefaultMaxWait() : userMax.intValue(); 456 457 GenericObjectPool pool = new GenericObjectPool(null); 459 pool.setMaxActive(maxActive); 460 pool.setMaxIdle(maxIdle); 461 pool.setMaxWait(maxWait); 462 pool.setWhenExhaustedAction(whenExhaustedAction(maxActive, maxWait)); 463 pool.setTestOnBorrow(getTestOnBorrow()); 464 pool.setTestOnReturn(getTestOnReturn()); 465 pool.setTimeBetweenEvictionRunsMillis( 466 getTimeBetweenEvictionRunsMillis()); 467 pool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun()); 468 pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis()); 469 pool.setTestWhileIdle(getTestWhileIdle()); 470 471 new CPDSConnectionFactory(cpds, pool, getValidationQuery(), 475 username, password); 476 477 pools.put(getPoolKey(username), pool); 478 } 479 480 487 private void readObject(ObjectInputStream in) 488 throws IOException , ClassNotFoundException { 489 try 490 { 491 in.defaultReadObject(); 492 PerUserPoolDataSource oldDS = (PerUserPoolDataSource) 493 new PerUserPoolDataSourceFactory() 494 .getObjectInstance(getReference(), null, null, null); 495 this.pools = oldDS.pools; 496 } 497 catch (NamingException e) 498 { 499 throw new IOException ("NamingException: " + e); 500 } 501 } 502 } 503 | Popular Tags |