Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool(PoolableObjectFactory factory) 

Source Link

Document

Create a new GenericObjectPool using the specified factory.

Usage

From source file:com.nesscomputing.syslog4j.impl.pool.generic.GenericSyslogPoolFactory.java

public ObjectPool<AbstractSyslogWriter> createPool() throws SyslogRuntimeException {
    GenericObjectPool<AbstractSyslogWriter> genericPool = new GenericObjectPool<AbstractSyslogWriter>(this);

    configureGenericObjectPool(genericPool);

    return genericPool;
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImpl.java

/**
 * Creates a DataSource with connection pooling as provided by Apache DBCP
 *
 * @return a DataSource//  w ww  . j  ava 2  s  .  co  m
 */
public static DataSource configureAndCreateDataSource() {

    log.debug("Configuring DataSource wrapped in a Database Connection Pool, using custom loader");

    GlobalConfiguration globalConfiguration = GlobalConfigurationImpl.getInstance();

    String jdbcDriverClassPath = globalConfiguration.getJdbcDriverClassPath();

    log.debug("Loading JDBC Driver with custom class path: " + jdbcDriverClassPath);
    // Creates a new class loader, which will be used for loading our JDBC driver
    URLClassLoader urlClassLoader = getOxalisClassLoaderForJdbc(jdbcDriverClassPath);

    String className = globalConfiguration.getJdbcDriverClassName();
    String connectURI = globalConfiguration.getJdbcConnectionURI();
    String userName = globalConfiguration.getJdbcUsername();
    String password = globalConfiguration.getJdbcPassword();

    log.debug("className=" + className);
    log.debug("connectURI=" + connectURI);
    log.debug("userName=" + userName);
    log.debug("password=" + password);

    // Loads the JDBC Driver in a separate class loader
    Driver driver = getJdbcDriver(jdbcDriverClassPath, urlClassLoader, className);

    Properties properties = new Properties();
    properties.put("user", userName);
    properties.put("password", password);

    // DBCP factory which will produce JDBC Driver instances
    ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, connectURI, properties);

    // DBCP object pool holding our driver connections
    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(100);
    genericObjectPool.setMaxIdle(30);
    genericObjectPool.setMaxWait(10000);

    genericObjectPool.setTestOnBorrow(true); // Test the connection returned from the pool

    genericObjectPool.setTestWhileIdle(true); // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
    genericObjectPool.setTimeBetweenEvictionRunsMillis(60 * 60 * 1000); // Test every hour

    // DBCP Factory holding the pooled connection, which are created by the driver connection factory and held in the supplied pool
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(driverConnectionFactory,
            genericObjectPool, null, null, false, true);

    String validationQuery = globalConfiguration.getValidationQuery();
    poolableConnectionFactory.setValidationQuery(validationQuery);

    // Creates the actual DataSource instance
    PoolingDataSource poolingDataSource = new PoolingDataSource(genericObjectPool);

    return poolingDataSource;

}

From source file:com.google.acre.keystore.MySQLKeyStore.java

@SuppressWarnings("unused")
private void setupDriver(String connectURI, String username, String password)
        throws SQLException, ClassNotFoundException {

    GenericObjectPool connectionPool = new GenericObjectPool(null);
    connectionPool.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000 - 13); // check if jdbc connections are still alive every 5 min - 13 msec

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);

    new PoolableConnectionFactory(connectionFactory, connectionPool, null, TEST_QUERY, false, true);

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX);

    driver.registerPool(DATASOURCE_NAME, connectionPool);

    active = true;//  ww  w . jav  a 2  s.c  om

    // Now we can just use the connect string "jdbc:apache:commons:dbcp:keystore"
    // to access our pool of Connections.
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

protected ObjectPool makeEmptyPool(int mincap) {
    GenericObjectPool pool = new GenericObjectPool(new SimpleFactory());
    pool.setMaxActive(mincap);/*from   w  w w.j  a  v a2 s  . co m*/
    pool.setMaxIdle(mincap);
    return pool;
}

From source file:backtype.storm.scheduler.adaptive.DataManager.java

private DataManager() {
    logger = Logger.getLogger(DataManager.class);
    logger.info("Starting DataManager (working directory: " + System.getProperty("user.dir") + ")");

    try {//from www.j a va  2  s.  c  om
        // load configuration from file
        logger.debug("Loading configuration from file");
        Properties properties = new Properties();
        properties.load(new FileInputStream("db.ini"));
        logger.debug("Configuration loaded");

        // load JDBC driver
        logger.debug("Loading JDBC driver");
        String jdbc_driver = properties.getProperty("jdbc.driver").trim();
        Class.forName(jdbc_driver);
        logger.debug("Driver loaded");

        // set up data source
        logger.debug("Setting up pooling data source");
        String connection_uri = properties.getProperty("data.connection.uri");
        String validation_query = properties.getProperty("validation.query");
        ObjectPool connectionPool = new GenericObjectPool(null);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connection_uri, null);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, validation_query, false, true);
        poolableConnectionFactory.hashCode();
        dataSource = new PoolingDataSource(connectionPool);
        logger.debug("Data source set up");

        nodeName = properties.getProperty("node-name");
        if (properties.getProperty("capacity") != null) {
            capacity = Integer.parseInt(properties.getProperty("capacity"));
            if (capacity < 1 || capacity > 100)
                throw new RuntimeException("Wrong capacity: " + capacity + ", expected in the range [1, 100]");
        }

        logger.info("DataManager started");
    } catch (Exception e) {
        logger.error("Error starting DataManager", e);
    }
}

From source file:net.ontopia.topicmaps.impl.rdbms.RDBMSTopicMapReference.java

protected void init() {
    // store factory
    TopicMapStoreFactoryIF sfactory = new TopicMapStoreFactoryIF() {
        public TopicMapStoreIF createStore() {
            return _createStore(false);
        }/*from   w  w  w.  jav  a 2s  . co  m*/
    };

    // initialize pool
    this.ofactory = new StorePoolableObjectFactory(sfactory);
    this.pool = new GenericObjectPool(ofactory);
    this.pool.setTestOnBorrow(true);

    Map properties = storage.getProperties();
    if (properties != null) {
        // Set minimum pool size (default: 0)
        String _minsize = PropertyUtils.getProperty(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.MinimumSize", false);
        int minsize = (_minsize == null ? 0 : Integer.parseInt(_minsize));
        log.debug("Setting StorePool.MinimumSize '" + minsize + "'");
        pool.setMinIdle(minsize); // 0 = no limit

        // Set maximum pool size (default: Integer.MAX_VALUE)
        String _maxsize = PropertyUtils.getProperty(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.MaximumSize", false);
        int maxsize = (_maxsize == null ? 8 : Integer.parseInt(_maxsize));
        log.debug("Setting StorePool.MaximumSize '" + maxsize + "'");
        pool.setMaxActive(maxsize); // 0 = no limit

        // Set soft maximum - emergency objects (default: false)
        boolean softmax = PropertyUtils.isTrue(properties,
                "net.ontopia.topicmaps.impl.rdbms.StorePool.SoftMaximum", false);
        log.debug("Setting StorePool.SoftMaximum '" + softmax + "'");
        if (softmax)
            pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        else
            pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    }

    // allow the user to fully overwrite exhausted options
    String _whenExhaustedAction = PropertyUtils.getProperty(properties,
            "net.ontopia.topicmaps.impl.rdbms.StorePool.WhenExhaustedAction", false);
    if (EXHAUSED_BLOCK.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    if (EXHAUSED_GROW.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    if (EXHAUSED_FAIL.equals(_whenExhaustedAction))
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL);

    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
        log.debug("Pool is set to block on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_GROW)
        log.debug("Pool is set to grow on exhaused");
    if (pool.getWhenExhaustedAction() == GenericObjectPool.WHEN_EXHAUSTED_FAIL)
        log.debug("Pool is set to fail on exhaused");

}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.service.impl.TibcoDriverManagerImpl.java

public Connection unlockConnection(DataSource dataSource) throws SQLException {
    Connection originalConnection = getOriginalConnection(dataSource);
    if (unlockDSConnection(originalConnection))
        return originalConnection;
    GenericObjectPool connectionPool = new GenericObjectPool(null);
    DataSourceConnectionFactory connectionFactory = new DataSourceConnectionFactory(dataSource);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    PoolingDataSource ds = new PoolingDataSource(connectionPool);
    if (ds != null) {
        Connection connection = getOriginalConnection(ds);
        ds.setAccessToUnderlyingConnectionAllowed(true);
        if (unlockDSConnection(connection))
            return connection;
    }//from  w  w  w .  j a va  2s. c  om
    return originalConnection;
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

protected ObjectPool makeEmptyPool(final PoolableObjectFactory factory) {
    return new GenericObjectPool(factory);
}

From source file:eu.peppol.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

@Test
public void testHandleStaleConnections() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(true);

    GenericObjectPool genericObjectPool = new GenericObjectPool(null);
    genericObjectPool.setMaxActive(1);/*w w w  . j  a  v  a  2  s.  c om*/

    genericObjectPool.setTestOnBorrow(true);
    /*
            genericObjectPool.setTestWhileIdle(true);   // Test idle instances visited by the pool maintenance thread and destroy any that fail validation
            genericObjectPool.setTimeBetweenEvictionRunsMillis(10000);      // Test every 10 seconds
            genericObjectPool.setMaxWait(10000);
    */

    PoolingDataSource poolingDataSource = createPoolingDataSource(driverConnectionFactory, genericObjectPool);
    runTwoSqlStatementsWithTwoConnections(poolingDataSource);
}

From source file:edu.illinois.enforcemop.examples.apache.pool.TestGenericObjectPool.java

public void setUp() throws Exception {
    //super.setUp();
    pool = new GenericObjectPool(new SimpleFactory());
}