1 31 32 package org.opencms.db; 33 34 import org.opencms.main.CmsLog; 35 import org.opencms.util.CmsStringUtil; 36 37 import java.sql.Connection ; 38 import java.util.ArrayList ; 39 import java.util.List ; 40 import java.util.Map ; 41 42 import org.apache.commons.collections.ExtendedProperties; 43 import org.apache.commons.dbcp.ConnectionFactory; 44 import org.apache.commons.dbcp.DriverManagerConnectionFactory; 45 import org.apache.commons.dbcp.PoolableConnectionFactory; 46 import org.apache.commons.dbcp.PoolingDriver; 47 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 48 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; 49 import org.apache.commons.pool.impl.GenericObjectPool; 50 51 67 public final class CmsDbPool { 68 69 70 public static final String DBCP_JDBC_URL_PREFIX = "jdbc:apache:commons:dbcp:"; 71 72 73 public static final String KEY_DATABASE = "db."; 74 75 76 public static final String KEY_DATABASE_NAME = KEY_DATABASE + "name"; 77 78 79 public static final String KEY_DATABASE_POOL = KEY_DATABASE + "pool"; 80 81 82 public static final String KEY_DATABASE_STATEMENTS = KEY_DATABASE + "statements"; 83 84 85 public static final String KEY_JDBC_DRIVER = "jdbcDriver"; 86 87 88 public static final String KEY_JDBC_URL = "jdbcUrl"; 89 90 91 public static final String KEY_JDBC_URL_PARAMS = KEY_JDBC_URL + ".params"; 92 93 94 public static final String KEY_MAX_ACTIVE = "maxActive"; 95 96 97 public static final String KEY_MAX_IDLE = "maxIdle"; 98 99 100 public static final String KEY_MAX_WAIT = "maxWait"; 101 102 103 public static final String KEY_MIN_EVICTABLE_IDLE_TIME = "minEvictableIdleTime"; 104 105 106 public static final String KEY_MIN_IDLE = "minIdle"; 107 108 109 public static final String KEY_NUM_TESTS_PER_EVICTION_RUN = "numTestsPerEvictionRun"; 110 111 112 public static final String KEY_PASSWORD = "password"; 113 114 115 public static final String KEY_POOL_DEFAULT = "default"; 116 117 118 public static final String KEY_POOL_URL = "poolUrl"; 119 120 121 public static final String KEY_POOL_USER = "user"; 122 123 124 public static final String KEY_POOL_VFS = "vfs"; 125 126 127 public static final String KEY_POOLING = "pooling"; 128 129 130 public static final String KEY_TEST_ON_BORROW = "testOnBorrow"; 131 132 133 public static final String KEY_TEST_QUERY = "testQuery"; 134 135 136 public static final String KEY_TEST_WHILE_IDLE = "testWhileIdle"; 137 138 139 public static final String KEY_TIME_BETWEEN_EVICTION_RUNS = "timeBetweenEvictionRuns"; 140 141 142 public static final String KEY_USERNAME = "user"; 143 144 145 public static final String KEY_WHEN_EXHAUSTED_ACTION = "whenExhaustedAction"; 146 147 148 public static final String OPENCMS_DEFAULT_POOL_NAME = "default"; 149 150 151 public static final String OPENCMS_DEFAULT_POOL_URL = "opencms:default"; 152 153 154 public static final String OPENCMS_URL_PREFIX = "opencms:"; 155 156 161 private CmsDbPool() { 162 163 super(); 164 } 165 166 174 public static PoolingDriver createDriverManagerConnectionPool(Map configuration, String key) throws Exception { 175 176 ExtendedProperties config; 177 if (configuration instanceof ExtendedProperties) { 178 config = (ExtendedProperties)configuration; 179 } else { 180 config = new ExtendedProperties(); 181 config.putAll(configuration); 182 } 183 184 String jdbcDriver = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_DRIVER); 186 String jdbcUrl = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL); 187 String jdbcUrlParams = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL_PARAMS); 188 int maxActive = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_ACTIVE, 10); 189 int maxWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_WAIT, 2000); 190 int maxIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_IDLE, 5); 191 int minEvictableIdleTime = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_EVICTABLE_IDLE_TIME, 1800000); 192 int minIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_IDLE, 0); 193 int numTestsPerEvictionRun = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_NUM_TESTS_PER_EVICTION_RUN, 3); 194 int timeBetweenEvictionRuns = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TIME_BETWEEN_EVICTION_RUNS, 3600000); 195 String testQuery = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_QUERY); 196 String username = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_USERNAME); 197 String password = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_PASSWORD); 198 String poolUrl = config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_POOL_URL); 199 String whenExhaustedActionValue = config.getString( 200 KEY_DATABASE_POOL + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION).trim(); 201 byte whenExhaustedAction = 0; 202 boolean testOnBorrow = Boolean.valueOf( 203 config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_ON_BORROW, "false").trim()).booleanValue(); 204 boolean testWhileIdle = Boolean.valueOf( 205 config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_WHILE_IDLE, "false").trim()).booleanValue(); 206 207 if ("block".equalsIgnoreCase(whenExhaustedActionValue)) { 208 whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; 209 } else if ("fail".equalsIgnoreCase(whenExhaustedActionValue)) { 210 whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; 211 } else if ("grow".equalsIgnoreCase(whenExhaustedActionValue)) { 212 whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; 213 } else { 214 whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; 215 } 216 217 if ("".equals(testQuery)) { 218 testQuery = null; 219 } 220 221 if (username == null) { 222 username = ""; 223 } 224 225 if (password == null) { 226 password = ""; 227 } 228 229 boolean poolingStmts = Boolean.valueOf( 231 config.getString(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_POOLING, CmsStringUtil.TRUE).trim()).booleanValue(); 232 int maxActiveStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_ACTIVE, 25); 233 int maxWaitStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_WAIT, 250); 234 int maxIdleStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_IDLE, 15); 235 String whenStmtsExhaustedActionValue = config.getString(KEY_DATABASE_STATEMENTS 236 + '.' 237 + key 238 + '.' 239 + KEY_WHEN_EXHAUSTED_ACTION); 240 byte whenStmtsExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW; 241 if (whenStmtsExhaustedActionValue != null) { 242 whenStmtsExhaustedActionValue = whenStmtsExhaustedActionValue.trim(); 243 whenStmtsExhaustedAction = ("block".equalsIgnoreCase(whenStmtsExhaustedActionValue)) ? GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK 244 : ("fail".equalsIgnoreCase(whenStmtsExhaustedActionValue)) ? GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL 245 : GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW; 246 } 247 248 Class.forName(jdbcDriver).newInstance(); 250 251 GenericObjectPool connectionPool = new GenericObjectPool(null); 253 254 263 264 connectionPool.setMaxActive(maxActive); 271 connectionPool.setMaxIdle(maxIdle); 272 connectionPool.setMinIdle(minIdle); 273 connectionPool.setMaxWait(maxWait); 274 connectionPool.setWhenExhaustedAction(whenExhaustedAction); 275 276 if (testQuery != null) { 277 connectionPool.setTestOnBorrow(testOnBorrow); 278 connectionPool.setTestWhileIdle(testWhileIdle); 279 connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns); 280 connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun); 281 connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTime); 282 } 283 284 if (jdbcUrlParams != null) { 286 jdbcUrl += jdbcUrlParams; 287 } 288 289 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, username, password); 290 291 GenericKeyedObjectPoolFactory statementFactory = null; 293 if (poolingStmts) { 294 statementFactory = new GenericKeyedObjectPoolFactory( 295 null, 296 maxActiveStmts, 297 whenStmtsExhaustedAction, 298 maxWaitStmts, 299 maxIdleStmts); 300 } 301 302 new PoolableConnectionFactory(connectionFactory, connectionPool, statementFactory, testQuery, false, true); 304 305 PoolingDriver driver = new PoolingDriver(); 307 driver.registerPool(poolUrl, connectionPool); 308 309 Connection con = connectionFactory.createConnection(); 311 con.close(); 312 313 if (CmsLog.INIT.isInfoEnabled()) { 314 CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_JDBC_POOL_2, poolUrl, jdbcUrl)); 315 } 316 return driver; 317 } 318 319 326 public static String getDbPoolName(Map configuration, String key) { 327 328 return configuration.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_POOL_URL).toString(); 331 332 340 } 341 342 348 public static List getDbPoolNames(Map configuration) { 349 350 ExtendedProperties config; 352 if (configuration instanceof ExtendedProperties) { 353 config = (ExtendedProperties)configuration; 354 } else { 355 config = new ExtendedProperties(); 356 config.putAll(configuration); 357 } 358 359 List dbPoolNames = new ArrayList (); 360 String [] driverPoolNames = config.getStringArray(CmsDriverManager.CONFIGURATION_DB + ".pools"); 361 362 for (int i = 0; i < driverPoolNames.length; i++) { 363 dbPoolNames.add(getDbPoolName(configuration, driverPoolNames[i])); 364 } 365 366 return dbPoolNames; 367 } 368 369 374 public static String getDefaultDbPoolName() { 375 376 return OPENCMS_DEFAULT_POOL_NAME; 377 } 378 } | Popular Tags |