List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool
public GenericObjectPool(PoolableObjectFactory factory, int maxActive, byte whenExhaustedAction, long maxWait, int maxIdle, int minIdle, boolean testOnBorrow, boolean testOnReturn, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, long minEvictableIdleTimeMillis, boolean testWhileIdle)
From source file:com.flipkart.phantom.thrift.impl.ThriftProxy.java
/** * Initialize this ThriftProxy//from w ww . j a v a2 s.com */ public void init(TaskContext context) throws Exception { if (this.thriftServiceClass == null) { throw new AssertionError("The 'thriftServiceClass' may not be null"); } if (this.processMap == null || this.processMap.isEmpty()) { throw new AssertionError( "ProcessFunctions not populated. Maybe The 'thriftServiceClass' is not a valid class?"); } if (this.thriftTimeoutMillis == -1) { // implying none set throw new Exception("'thriftTimeoutMillis' must be set to a non-negative value!"); } //Create pool this.socketPool = new GenericObjectPool<Socket>(new SocketObjectFactory(this), this.poolSize, GenericObjectPool.WHEN_EXHAUSTED_GROW, this.maxWait, this.maxIdle, this.minIdle, false, false, this.timeBetweenEvictionRunsMillis, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, true); }
From source file:org.opensubsystems.core.persist.jdbc.connectionpool.dbcp.DBCPDatabaseConnectionFactoryImpl.java
/** * {@inheritDoc}/*from w w w .j a v a 2 s . c om*/ */ @Override protected Object createConnectionPool(String strConnectionPoolName, Database database, String strDriverName, String strUrl, String strUser, String strPassword, int iTransactionIsolation) throws OSSException { // I am using here the PoolingDriver instead of PoolingDataSource because // in DBCP version 1.1 the PoolingDriver has clear way how to shutdown // the pool and PoolingDataSource doesn't. // This code was inspired by method setupDriver from // ManualPoolingDriverExample.java in commons-dbcp package v 1.6 ObjectPool connectionPool; ConnectionFactory connectionFactory; PoolableConnectionFactory poolableConnectionFactory; PooledDatabaseConnectionFactorySetupReader setupReader = new PooledDatabaseConnectionFactorySetupReader( strConnectionPoolName, database.getDatabaseTypeIdentifier()); int iInitialPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_INITIAL_SIZE) .intValue(); int iMinimalPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_MIN_SIZE).intValue(); int iMaximalPoolSize = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_MAX_SIZE).intValue(); boolean bCanGrow = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_CAN_GROW) .booleanValue(); long lMaxWaitTimeForConnection = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_WAIT_PERIOD).longValue(); boolean bValidateOnBorrow = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_BORROW) .booleanValue(); boolean bValidateOnReturn = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_RETURN) .booleanValue(); boolean bValidateOnIdle = setupReader .getBooleanParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_VALIDATE_IDLE) .booleanValue(); long lTimeBetweenEvictionRunsMillis = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_PERIOD) .longValue(); int iNumTestsPerEvictionRun = setupReader .getIntegerParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_CHECK_SIZE) .intValue(); long lMinEvictableIdleTimeMillis = setupReader .getLongParameterValue(PooledDatabaseConnectionFactorySetupReader.DBPOOL_IDLE_PERIOD).longValue(); int iPreparedStatementCacheSize = setupReader.getIntegerParameterValue( PooledDatabaseConnectionFactorySetupReader.DBPOOL_PREPSTATEMENT_CACHE_SIZE).intValue(); // First, we'll need a ObjectPool that serves as the actual pool of // connections. We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. connectionPool = new GenericObjectPool(null, // factory will be specified below iMaximalPoolSize, bCanGrow ? GenericObjectPool.WHEN_EXHAUSTED_GROW : GenericObjectPool.WHEN_EXHAUSTED_BLOCK, lMaxWaitTimeForConnection, iMaximalPoolSize, // max idle - if no connections are used // the pool should not fall under this size iMinimalPoolSize, // min idle - if connection count falls // under this limit (e.g. closed connections) // new connections will be created bValidateOnBorrow, bValidateOnReturn, lTimeBetweenEvictionRunsMillis, iNumTestsPerEvictionRun, lMinEvictableIdleTimeMillis, bValidateOnIdle); // Next, we'll create a ConnectionFactory that the pool will use to // create Connections. I am using DriverManagerConnectionFactory instead // of DriverConnectionFactory because it allows me to specify user name // and password directly connectionFactory = new DriverManagerConnectionFactory(strUrl, strUser, strPassword); // This configuration of prepared statement caching is inspired by // Commons-DBCP Wiki available at http://wiki.apache.org/jakarta-commons/DBCP // null can be used as parameter because this parameter is set in // PoolableConnectionFactory when creating a new PoolableConnection // 0 according to documentation should mean no limit KeyedObjectPoolFactory statementPool = null; if (iPreparedStatementCacheSize >= 0) { statementPool = new GenericKeyedObjectPoolFactory(null, iPreparedStatementCacheSize); } // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, statementPool, DatabaseImpl.getInstance().getConnectionTestStatement(), false, // not read-only connection false, // Default auto commit is false iTransactionIsolation); // PoolableConnectionFactory doesn't support the initialSize attribute of // DBCP so I have replicated the code from BasicDataSource v1.37 here try { for (int iIndex = 0; iIndex < iInitialPoolSize; iIndex++) { connectionPool.addObject(); } } catch (Exception e) { throw new OSSDatabaseAccessException("Error preloading the connection pool", e); } if (GlobalConstants.ERROR_CHECKING) { // Put this check here to trick checkstyle assert poolableConnectionFactory != null : "Poolable connection factory cannot be null."; } return connectionPool; }