List of usage examples for org.apache.commons.pool.impl GenericObjectPool DEFAULT_WHEN_EXHAUSTED_ACTION
byte DEFAULT_WHEN_EXHAUSTED_ACTION
To view the source code for org.apache.commons.pool.impl GenericObjectPool DEFAULT_WHEN_EXHAUSTED_ACTION.
Click Source Link
From source file:com.fjn.helper.frameworkex.apache.commons.pool.connectionPool.ConnectionManager.java
/** * * @param maxNum//from w w w . j a va2 s. c o m * @param maxIdleCount * @param minIdleCount * @param maxWaitTime * */ public void initConnectionPool(int maxNum, int maxIdleCount, int minIdleCount, long maxWaitTime) { int maxActive = maxNum; byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; long maxWait = maxWaitTime; int maxIdle = maxIdleCount; int minIdle = minIdleCount; boolean testOnBorrow = GenericObjectPool.DEFAULT_TEST_ON_BORROW; boolean testOnReturn = GenericObjectPool.DEFAULT_TEST_ON_RETURN; long timeBetweenEvictionRunsMillis = GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS; int numTestsPerEvictionRun = GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN; long minEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS; boolean testWhileIdle = GenericObjectPool.DEFAULT_TEST_WHILE_IDLE; long softMinEvictableIdleTimeMillis = GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS; boolean lifo = GenericObjectPool.DEFAULT_LIFO; connPoolFactory = new ConnectionPoolFactory(connFactory, maxActive, whenExhaustedAction, maxWait, maxIdle, minIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle, softMinEvictableIdleTimeMillis, lifo); }
From source file:com.cloud.utils.db.TransactionLegacy.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public static void initDataSource(Properties dbProps) { try {// ww w . j a v a 2 s. co m if (dbProps.size() == 0) return; s_dbHAEnabled = Boolean.valueOf(dbProps.getProperty("db.ha.enabled")); s_logger.info("Is Data Base High Availiability enabled? Ans : " + s_dbHAEnabled); String loadBalanceStrategy = dbProps.getProperty("db.ha.loadBalanceStrategy"); // FIXME: If params are missing...default them???? final int cloudMaxActive = Integer.parseInt(dbProps.getProperty("db.cloud.maxActive")); final int cloudMaxIdle = Integer.parseInt(dbProps.getProperty("db.cloud.maxIdle")); final long cloudMaxWait = Long.parseLong(dbProps.getProperty("db.cloud.maxWait")); final String cloudUsername = dbProps.getProperty("db.cloud.username"); final String cloudPassword = dbProps.getProperty("db.cloud.password"); final String cloudHost = dbProps.getProperty("db.cloud.host"); final String cloudDriver = dbProps.getProperty("db.cloud.driver"); final int cloudPort = Integer.parseInt(dbProps.getProperty("db.cloud.port")); final String cloudDbName = dbProps.getProperty("db.cloud.name"); final boolean cloudAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.cloud.autoReconnect")); final String cloudValidationQuery = dbProps.getProperty("db.cloud.validationQuery"); final String cloudIsolationLevel = dbProps.getProperty("db.cloud.isolation.level"); int isolationLevel = Connection.TRANSACTION_READ_COMMITTED; if (cloudIsolationLevel == null) { isolationLevel = Connection.TRANSACTION_READ_COMMITTED; } else if (cloudIsolationLevel.equalsIgnoreCase("readcommitted")) { isolationLevel = Connection.TRANSACTION_READ_COMMITTED; } else if (cloudIsolationLevel.equalsIgnoreCase("repeatableread")) { isolationLevel = Connection.TRANSACTION_REPEATABLE_READ; } else if (cloudIsolationLevel.equalsIgnoreCase("serializable")) { isolationLevel = Connection.TRANSACTION_SERIALIZABLE; } else if (cloudIsolationLevel.equalsIgnoreCase("readuncommitted")) { isolationLevel = Connection.TRANSACTION_READ_UNCOMMITTED; } else { s_logger.warn("Unknown isolation level " + cloudIsolationLevel + ". Using read uncommitted"); } final boolean cloudTestOnBorrow = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testOnBorrow")); final boolean cloudTestWhileIdle = Boolean.parseBoolean(dbProps.getProperty("db.cloud.testWhileIdle")); final long cloudTimeBtwEvictionRunsMillis = Long .parseLong(dbProps.getProperty("db.cloud.timeBetweenEvictionRunsMillis")); final long cloudMinEvcitableIdleTimeMillis = Long .parseLong(dbProps.getProperty("db.cloud.minEvictableIdleTimeMillis")); final boolean cloudPoolPreparedStatements = Boolean .parseBoolean(dbProps.getProperty("db.cloud.poolPreparedStatements")); final String url = dbProps.getProperty("db.cloud.url.params"); String cloudDbHAParams = null; String cloudSlaves = null; if (s_dbHAEnabled) { cloudDbHAParams = getDBHAParams("cloud", dbProps); cloudSlaves = dbProps.getProperty("db.cloud.slaves"); s_logger.info("The slaves configured for Cloud Data base is/are : " + cloudSlaves); } final boolean useSSL = Boolean.parseBoolean(dbProps.getProperty("db.cloud.useSSL")); if (useSSL) { System.setProperty("javax.net.ssl.keyStore", dbProps.getProperty("db.cloud.keyStore")); System.setProperty("javax.net.ssl.keyStorePassword", dbProps.getProperty("db.cloud.keyStorePassword")); System.setProperty("javax.net.ssl.trustStore", dbProps.getProperty("db.cloud.trustStore")); System.setProperty("javax.net.ssl.trustStorePassword", dbProps.getProperty("db.cloud.trustStorePassword")); } final GenericObjectPool cloudConnectionPool = new GenericObjectPool(null, cloudMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, cloudMaxWait, cloudMaxIdle, cloudTestOnBorrow, false, cloudTimeBtwEvictionRunsMillis, 1, cloudMinEvcitableIdleTimeMillis, cloudTestWhileIdle); final String cloudConnectionUri = cloudDriver + "://" + cloudHost + (s_dbHAEnabled ? "," + cloudSlaves : "") + ":" + cloudPort + "/" + cloudDbName + "?autoReconnect=" + cloudAutoReconnect + (url != null ? "&" + url : "") + (useSSL ? "&useSSL=true" : "") + (s_dbHAEnabled ? "&" + cloudDbHAParams : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""); DriverLoader.loadDriver(cloudDriver); final ConnectionFactory cloudConnectionFactory = new DriverManagerConnectionFactory(cloudConnectionUri, cloudUsername, cloudPassword); final KeyedObjectPoolFactory poolableObjFactory = (cloudPoolPreparedStatements ? new StackKeyedObjectPoolFactory() : null); final PoolableConnectionFactory cloudPoolableConnectionFactory = new PoolableConnectionFactory( cloudConnectionFactory, cloudConnectionPool, poolableObjFactory, cloudValidationQuery, false, false, isolationLevel); // Default Data Source for CloudStack s_ds = new PoolingDataSource(cloudPoolableConnectionFactory.getPool()); // Configure the usage db final int usageMaxActive = Integer.parseInt(dbProps.getProperty("db.usage.maxActive")); final int usageMaxIdle = Integer.parseInt(dbProps.getProperty("db.usage.maxIdle")); final long usageMaxWait = Long.parseLong(dbProps.getProperty("db.usage.maxWait")); final String usageUsername = dbProps.getProperty("db.usage.username"); final String usagePassword = dbProps.getProperty("db.usage.password"); final String usageHost = dbProps.getProperty("db.usage.host"); final String usageDriver = dbProps.getProperty("db.usage.driver"); final int usagePort = Integer.parseInt(dbProps.getProperty("db.usage.port")); final String usageDbName = dbProps.getProperty("db.usage.name"); final boolean usageAutoReconnect = Boolean.parseBoolean(dbProps.getProperty("db.usage.autoReconnect")); final String usageUrl = dbProps.getProperty("db.usage.url.params"); final GenericObjectPool usageConnectionPool = new GenericObjectPool(null, usageMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, usageMaxWait, usageMaxIdle); final String usageConnectionUri = usageDriver + "://" + usageHost + (s_dbHAEnabled ? "," + dbProps.getProperty("db.cloud.slaves") : "") + ":" + usagePort + "/" + usageDbName + "?autoReconnect=" + usageAutoReconnect + (usageUrl != null ? "&" + usageUrl : "") + (s_dbHAEnabled ? "&" + getDBHAParams("usage", dbProps) : "") + (s_dbHAEnabled ? "&loadBalanceStrategy=" + loadBalanceStrategy : ""); DriverLoader.loadDriver(usageDriver); final ConnectionFactory usageConnectionFactory = new DriverManagerConnectionFactory(usageConnectionUri, usageUsername, usagePassword); final PoolableConnectionFactory usagePoolableConnectionFactory = new PoolableConnectionFactory( usageConnectionFactory, usageConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false); // Data Source for usage server s_usageDS = new PoolingDataSource(usagePoolableConnectionFactory.getPool()); try { // Configure the simulator db final int simulatorMaxActive = Integer.parseInt(dbProps.getProperty("db.simulator.maxActive")); final int simulatorMaxIdle = Integer.parseInt(dbProps.getProperty("db.simulator.maxIdle")); final long simulatorMaxWait = Long.parseLong(dbProps.getProperty("db.simulator.maxWait")); final String simulatorUsername = dbProps.getProperty("db.simulator.username"); final String simulatorPassword = dbProps.getProperty("db.simulator.password"); final String simulatorHost = dbProps.getProperty("db.simulator.host"); final String simulatorDriver = dbProps.getProperty("db.simulator.driver"); final int simulatorPort = Integer.parseInt(dbProps.getProperty("db.simulator.port")); final String simulatorDbName = dbProps.getProperty("db.simulator.name"); final boolean simulatorAutoReconnect = Boolean .parseBoolean(dbProps.getProperty("db.simulator.autoReconnect")); final GenericObjectPool simulatorConnectionPool = new GenericObjectPool(null, simulatorMaxActive, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION, simulatorMaxWait, simulatorMaxIdle); final String simulatorConnectionUri = simulatorDriver + "://" + simulatorHost + ":" + simulatorPort + "/" + simulatorDbName + "?autoReconnect=" + simulatorAutoReconnect; DriverLoader.loadDriver(simulatorDriver); final ConnectionFactory simulatorConnectionFactory = new DriverManagerConnectionFactory( simulatorConnectionUri, simulatorUsername, simulatorPassword); final PoolableConnectionFactory simulatorPoolableConnectionFactory = new PoolableConnectionFactory( simulatorConnectionFactory, simulatorConnectionPool, new StackKeyedObjectPoolFactory(), null, false, false); s_simulatorDS = new PoolingDataSource(simulatorPoolableConnectionFactory.getPool()); } catch (Exception e) { s_logger.debug("Simulator DB properties are not available. Not initializing simulator DS"); } } catch (final Exception e) { s_ds = getDefaultDataSource("cloud"); s_usageDS = getDefaultDataSource("cloud_usage"); s_simulatorDS = getDefaultDataSource("cloud_simulator"); s_logger.warn( "Unable to load db configuration, using defaults with 5 connections. Falling back on assumed datasource on localhost:3306 using username:password=cloud:cloud. Please check your configuration", e); } }
From source file:org.apache.avalon.dbcp.DbcpConnectionManager.java
/** * Returns an object pool configuration object populated with data * retrieved from the component's configuration. Defaults correspond * to the <code>GenericObjectPool</code> default values. * /* w w w . j a v a 2s.co m*/ * @return <code>GenericObjectPool.Config</code> instance containing * the pool's configuration parameters */ private GenericObjectPool.Config getPoolConfig() { GenericObjectPool.Config config = new GenericObjectPool.Config(); if (m_pool != null) { config.maxActive = m_pool.getAttributeAsInteger("max-active", GenericObjectPool.DEFAULT_MAX_ACTIVE); config.maxIdle = m_pool.getAttributeAsInteger("max-idle", GenericObjectPool.DEFAULT_MAX_IDLE); config.maxWait = m_pool.getAttributeAsLong("max-wait", GenericObjectPool.DEFAULT_MAX_WAIT); config.minEvictableIdleTimeMillis = m_pool.getAttributeAsLong("min-evict-idle-time", GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); config.minIdle = m_pool.getAttributeAsInteger("min-idle", GenericObjectPool.DEFAULT_MIN_IDLE); config.numTestsPerEvictionRun = m_pool.getAttributeAsInteger("num-evict-tests", GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); config.testOnBorrow = m_pool.getAttributeAsBoolean("test-on-borrow", GenericObjectPool.DEFAULT_TEST_ON_BORROW); config.testOnReturn = m_pool.getAttributeAsBoolean("test-on-return", GenericObjectPool.DEFAULT_TEST_ON_RETURN); config.testWhileIdle = m_pool.getAttributeAsBoolean("test-while-idle", GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); config.timeBetweenEvictionRunsMillis = m_pool.getAttributeAsLong("time-between-evict-runs", GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); config.whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; } return config; }
From source file:org.apache.cayenne.conf.CustomDBCPDataSourceBuilder.java
private KeyedObjectPoolFactory createPreparedStatementPool() { if (!config.getBoolean("poolPreparedStatements", false)) { return null; }//ww w . j a va 2 s.c o m // the GenericKeyedObjectPool.Config object isn't used because // although it has provision for the maxTotal parameter when // passed to the GenericKeyedObjectPoolFactory constructor // this parameter is not being properly set as a default for // creating prepared statement pools int maxActive = config.getInt(PS_MAX_ACTIVE, GenericObjectPool.DEFAULT_MAX_ACTIVE); byte whenExhaustedAction = config.getWhenExhaustedAction(PS_EXHAUSTED_ACTION, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION); long maxWait = config.getLong(PS_MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT); int maxIdle = config.getInt(PS_MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE); int maxTotal = config.getInt(PS_MAX_TOTAL, 1); boolean testOnBorrow = config.getBoolean(PS_TEST_ON_BORROW, GenericObjectPool.DEFAULT_TEST_ON_BORROW); boolean testOnReturn = config.getBoolean(PS_TEST_ON_RETURN, GenericObjectPool.DEFAULT_TEST_ON_RETURN); long timeBetweenEvictionRunsMillis = config.getLong(PS_TIME_BETWEEN_EVICTIONS, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); int numTestsPerEvictionRun = config.getInt(PS_NUM_TEST_PER_EVICTION, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); long minEvictableIdleTimeMillis = config.getLong(PS_MIN_EVICTABLE_TIME, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); boolean testWhileIdle = config.getBoolean(PS_TEST_IDLE, GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); return new GenericKeyedObjectPoolFactory(null, maxActive, whenExhaustedAction, maxWait, maxIdle, maxTotal, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); }
From source file:org.apache.cayenne.conf.CustomDBCPDataSourceBuilder.java
private GenericObjectPool.Config createConnectionPoolConfig() { GenericObjectPool.Config poolConfig = new GenericObjectPool.Config(); poolConfig.maxIdle = config.getInt(MAX_IDLE, GenericObjectPool.DEFAULT_MAX_IDLE); poolConfig.minIdle = config.getInt(MIN_IDLE, GenericObjectPool.DEFAULT_MIN_IDLE); poolConfig.maxActive = config.getInt(MAX_ACTIVE, GenericObjectPool.DEFAULT_MAX_ACTIVE); poolConfig.maxWait = config.getLong(MAX_WAIT, GenericObjectPool.DEFAULT_MAX_WAIT); poolConfig.testOnBorrow = config.getBoolean(TEST_ON_BORROW, GenericObjectPool.DEFAULT_TEST_ON_BORROW); poolConfig.testOnReturn = config.getBoolean(TEST_ON_RETURN, GenericObjectPool.DEFAULT_TEST_ON_RETURN); poolConfig.testWhileIdle = config.getBoolean(TEST_IDLE, GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); poolConfig.timeBetweenEvictionRunsMillis = config.getLong(TIME_BETWEEN_EVICTIONS, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); poolConfig.numTestsPerEvictionRun = config.getInt(NUM_TEST_PER_EVICTION, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); poolConfig.minEvictableIdleTimeMillis = config.getLong(MIN_EVICTABLE_TIME, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); poolConfig.whenExhaustedAction = config.getWhenExhaustedAction(EXHAUSTED_ACTION, GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION); return poolConfig; }
From source file:org.opencms.db.CmsDbPool.java
/** * Creates a JDBC DriverManager based DBCP connection pool.<p> * //from w w w . ja va 2s.c o m * @param config the configuration (opencms.properties) * @param key the key of the database pool in the configuration * @return String the URL to access the created DBCP pool * @throws Exception if the pool could not be initialized */ public static PoolingDriver createDriverManagerConnectionPool(CmsParameterConfiguration config, String key) throws Exception { // read the values of the pool configuration specified by the given key String jdbcDriver = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_DRIVER); String jdbcUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL); String jdbcUrlParams = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_JDBC_URL_PARAMS); int maxActive = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_ACTIVE, 10); int maxWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_WAIT, 2000); int maxIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MAX_IDLE, 5); int minEvictableIdleTime = config .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_EVICTABLE_IDLE_TIME, 1800000); int minIdle = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_MIN_IDLE, 0); int numTestsPerEvictionRun = config .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_NUM_TESTS_PER_EVICTION_RUN, 3); int timeBetweenEvictionRuns = config .getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TIME_BETWEEN_EVICTION_RUNS, 3600000); String testQuery = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_QUERY); String username = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_USERNAME); String password = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_PASSWORD); String poolUrl = config.get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_POOL_URL); String whenExhaustedActionValue = config .get(KEY_DATABASE_POOL + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION).trim(); byte whenExhaustedAction = 0; boolean testOnBorrow = Boolean .valueOf(config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_ON_BORROW, "false").trim()) .booleanValue(); boolean testWhileIdle = Boolean .valueOf( config.getString(KEY_DATABASE_POOL + '.' + key + '.' + KEY_TEST_WHILE_IDLE, "false").trim()) .booleanValue(); if ("block".equalsIgnoreCase(whenExhaustedActionValue)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; } else if ("fail".equalsIgnoreCase(whenExhaustedActionValue)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; } else if ("grow".equalsIgnoreCase(whenExhaustedActionValue)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; } else { whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; } if ("".equals(testQuery)) { testQuery = null; } if (username == null) { username = ""; } if (password == null) { password = ""; } // read the values of the statement pool configuration specified by the given key boolean poolingStmts = Boolean.valueOf(config .getString(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_POOLING, CmsStringUtil.TRUE).trim()) .booleanValue(); int maxActiveStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_ACTIVE, 25); int maxWaitStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_WAIT, 250); int maxIdleStmts = config.getInteger(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_MAX_IDLE, 15); String whenStmtsExhaustedActionValue = config .get(KEY_DATABASE_STATEMENTS + '.' + key + '.' + KEY_WHEN_EXHAUSTED_ACTION); byte whenStmtsExhaustedAction = GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW; if (whenStmtsExhaustedActionValue != null) { whenStmtsExhaustedActionValue = whenStmtsExhaustedActionValue.trim(); whenStmtsExhaustedAction = ("block".equalsIgnoreCase(whenStmtsExhaustedActionValue)) ? GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK : ("fail".equalsIgnoreCase(whenStmtsExhaustedActionValue)) ? GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL : GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW; } int connectionAttempts = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_ATTEMTS, 10); int connetionsWait = config.getInteger(KEY_DATABASE_POOL + '.' + key + '.' + KEY_CONNECT_WAITS, 5000); // create an instance of the JDBC driver Class.forName(jdbcDriver).newInstance(); // initialize a keyed object pool to store connections GenericObjectPool connectionPool = new GenericObjectPool(null); /* Abandoned pool configuration: * * In case the systems encounters "pool exhaustion" (runs out of connections), * comment the above line with "new GenericObjectPool(null)" and uncomment the * 5 lines below. This will generate an "abandoned pool" configuration that logs * abandoned connections to the System.out. Unfortunatly this code is deprecated, * so to avoid code warnings it's also disabled here. * Tested with commons-pool v 1.2. */ // AbandonedConfig abandonedConfig = new AbandonedConfig(); // abandonedConfig.setLogAbandoned(true); // abandonedConfig.setRemoveAbandoned(true); // abandonedConfig.setRemoveAbandonedTimeout(5); // GenericObjectPool connectionPool = new AbandonedObjectPool(null, abandonedConfig); // initialize an object pool to store connections connectionPool.setMaxActive(maxActive); connectionPool.setMaxIdle(maxIdle); connectionPool.setMinIdle(minIdle); connectionPool.setMaxWait(maxWait); connectionPool.setWhenExhaustedAction(whenExhaustedAction); if (testQuery != null) { connectionPool.setTestOnBorrow(testOnBorrow); connectionPool.setTestWhileIdle(testWhileIdle); connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns); connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun); connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTime); } // initialize a connection factory to make the DriverManager taking connections from the pool if (jdbcUrlParams != null) { jdbcUrl += jdbcUrlParams; } ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, username, password); // Set up statement pool, if desired GenericKeyedObjectPoolFactory statementFactory = null; if (poolingStmts) { statementFactory = new GenericKeyedObjectPoolFactory(null, maxActiveStmts, whenStmtsExhaustedAction, maxWaitStmts, maxIdleStmts); } // initialize a factory to obtain pooled connections and prepared statements new PoolableConnectionFactory(connectionFactory, connectionPool, statementFactory, testQuery, false, true); // initialize a new pooling driver using the pool PoolingDriver driver = new PoolingDriver(); driver.registerPool(poolUrl, connectionPool); Connection con = null; boolean connect = false; int connectionTests = 0; // try to connect once to the database to ensure it can be connected to at all // if the conection cannot be established, multiple attempts will be done to connect // just in cast the database was not fast enough to start before OpenCms was started do { try { // try to connect con = connectionFactory.createConnection(); connect = true; } catch (Exception e) { // connection failed, increase attempts, sleept for some seconds and log a message connectionTests++; if (CmsLog.INIT.isInfoEnabled()) { CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_WAIT_FOR_DB_4, new Object[] { poolUrl, jdbcUrl, new Integer(connectionTests), new Integer(connetionsWait) })); } Thread.sleep(connetionsWait); } finally { if (con != null) { con.close(); } } } while (!connect && (connectionTests < connectionAttempts)); if (CmsLog.INIT.isInfoEnabled()) { CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_JDBC_POOL_2, poolUrl, jdbcUrl)); } return driver; }
From source file:org.sakaiproject.nakamura.lite.storage.AbstractClientConnectionPool.java
@Activate public void activate(Map<String, Object> properties) throws ClassNotFoundException { // for testing purposes if (configuration == null) { configuration = (Configuration) properties.get(Configuration.class.getName()); }// ww w. ja v a 2 s . c o m indexColums = ImmutableSet.of(configuration.getIndexColumnNames()); int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200); byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; String whenExhausted = (String) properties.get(WHEN_EHAUSTED); if ("fail".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; } else if ("grow".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; } else if ("block".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; } long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L); int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5); boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true); boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true); long timeBetweenEvictionRunsMillis = StorageClientUtils .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L); int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN), 1000); long minEvictableIdleTimeMillis = StorageClientUtils .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L); boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false); pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); // set the maximum size of a string, if this is not 0, strings over this size will become files. StringType.setLengthLimit(StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), 0)); }
From source file:org.sakaiproject.nakamura.lite.storage.spi.AbstractClientConnectionPool.java
@Activate public void activate(Map<String, Object> properties) throws ClassNotFoundException { // for testing purposes if (configuration == null) { configuration = (Configuration) properties.get(Configuration.class.getName()); }/* w w w. ja v a 2 s . c o m*/ indexColumns = ImmutableSet.copyOf(configuration.getIndexColumnNames()); indexColumnsTypes = ImmutableSet.copyOf(configuration.getIndexColumnTypes()); int maxActive = StorageClientUtils.getSetting(properties.get(MAX_ACTIVE), 200); byte whenExhaustedAction = GenericObjectPool.DEFAULT_WHEN_EXHAUSTED_ACTION; String whenExhausted = (String) properties.get(WHEN_EHAUSTED); if ("fail".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL; } else if ("grow".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_GROW; } else if ("block".equals(whenExhausted)) { whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK; } long maxWait = StorageClientUtils.getSetting(properties.get(MAX_WAIT), 10L); int maxIdle = StorageClientUtils.getSetting(properties.get(MAX_IDLE), 5); boolean testOnBorrow = StorageClientUtils.getSetting(properties.get(TEST_ON_BORROW), true); boolean testOnReturn = StorageClientUtils.getSetting(properties.get(TEST_ON_RETURN), true); long timeBetweenEvictionRunsMillis = StorageClientUtils .getSetting(properties.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS), 60000L); int numTestsPerEvictionRun = StorageClientUtils.getSetting(properties.get(NUM_TESTS_PER_EVICTION_RUN), 1000); long minEvictableIdleTimeMillis = StorageClientUtils .getSetting(properties.get(MIN_EVICTABLE_IDLE_TIME_MILLIS), 10000L); boolean testWhileIdle = StorageClientUtils.getSetting(properties.get(TEST_WHILE_IDLE), false); pool = new GenericObjectPool(getConnectionPoolFactory(), maxActive, whenExhaustedAction, maxWait, maxIdle, testOnBorrow, testOnReturn, timeBetweenEvictionRunsMillis, numTestsPerEvictionRun, minEvictableIdleTimeMillis, testWhileIdle); // set the maximum size of a string, if this is not 0, strings over this size will become files. StringType.setLengthLimit( StorageClientUtils.getSetting(properties.get(LONG_STRING_SIZE), DEFAULT_LONG_STRING_SIZE)); // location of the long string store. LongString.setBase(StorageClientUtils.getSetting(properties.get(LONG_STRING_STORE_BASE), StorageClientUtils.getSetting(properties.get(FS_STORE_BASE_DIR), DEFAULT_FILE_STORE))); }