Example usage for org.apache.commons.dbcp PoolingDataSource PoolingDataSource

List of usage examples for org.apache.commons.dbcp PoolingDataSource PoolingDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp PoolingDataSource PoolingDataSource.

Prototype

public PoolingDataSource(ObjectPool pool) 

Source Link

Usage

From source file:storm.scheduler.DataManager.java

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

    try {//from   w ww .j a v a 2s  . co m
        // load configuration from file
        logger.debug("Loading configuration from file");
        Properties properties = new Properties();
        properties.load(new FileInputStream("/home/miller/research/storm/apache-storm-0.9.3/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, properties);
        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:ua.company.model.factory.PostgreDAOFactory.java

private void initSource(/*Logger logger*/) {

    // load the underlying driver
    try {//  www  . j a v a  2s.c o  m
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException ex) {
        logger.error(ex.getMessage());
        System.exit(1);
    }

    ConnectionFactory connectionFactory = null;
    ObjectPool connectionPool = null;
    PoolableConnectionFactory poolableConnectionFactory = null;

    // Build the DSN: jdbc:postgresql://host:port/database
    String buf = "jdbc:postgresql://127.0.0.1:5432/Restaurant";

    Properties props = new Properties();
    props.setProperty("user", "postgres");
    props.setProperty("password", "postgres");
    props.setProperty("initialSize", "50");

    props.setProperty("maxActive", "25");

    connectionFactory = new DriverManagerConnectionFactory(buf, props);
    connectionPool = new GenericObjectPool(null);
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null,
            false, true);

    dataSource = new PoolingDataSource(connectionPool);
}

From source file:uk.ac.sanger.cgp.dbcon.pooling.Pool.java

/**
 * Creates the pool and the data source which provides the pooling ability
 */// w  ww.j  a v  a 2s .co  m
private void createPool() {
    PoolableObjectFactory pof = null;

    // Creation of the generic pool and linking a factory to it
    GenericObjectPool underlyingConnectionPool = new GenericObjectPool(pof, config.getMaxActive(),
            config.getExhausted(), config.getMaxWait(), config.getMaxIdle(), config.isTestOnBorrow(),
            config.isTestOnReturn(), config.getTimeBetweenEvictRun(), config.getNumTestsPerEvictionRun(),
            config.getMinEvictTime(), config.isTestWhileIdle());

    setUnderlyingConnectionPool(underlyingConnectionPool);

    // This section allows for PreparedStatements to be used
    GenericKeyedObjectPoolFactory kopf = null;

    if (config.getCachedPreparedStatements() != 0) {
        kopf = new GenericKeyedObjectPoolFactory(null, -1, // unlimited maxActive
                // (per key)
                GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL, 0, // maxWait
                1, // maxIdle (per key)
                config.getCachedPreparedStatements());
    }

    // Creating the correct connection factory
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(config.getWorkingUrl(),
            config.getUsername(), config.getPassword());

    // Final construction of the poolable connection factory from:
    // PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
    // KeyedObjectPoolFactory stmtPoolFactory, String validationQuery,
    // boolean defaultReadOnly, boolean defaultAutoCommit)

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            getUnderlyingConnectionPool(), kopf, null, false, false);

    // Setting the validation query
    poolableConnectionFactory.setValidationQuery(config.getValidationQuery());

    // Creating the pooled datasource
    setDataSource(new PoolingDataSource(getUnderlyingConnectionPool()));
}

From source file:utility.ConnectionPool.java

/**
 *
 * @return @throws Exception/*from  w  w w  . j av  a  2s  . c o  m*/
 */
public DataSource setUp() throws Exception {
    /**
     * Load JDBC Driver class.
     */
    Class.forName(ConnectionPool.DRIVER).newInstance();

    /**
     * Creates an instance of GenericObjectPool that holds our pool of
     * connections object.
     */
    connectionPool = new GenericObjectPool();
    // set the max number of connections
    connectionPool.setMaxActive(connections);
    // if the pool is exhausted (i.e., the maximum number of active objects has been reached), the borrowObject() method should simply create a new object anyway
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);

    /**
     * Creates a connection factory object which will be use by the pool to
     * create the connection object. We passes the JDBC url info, username and
     * password.
     */
    ConnectionFactory cf = new DriverManagerConnectionFactory(URL, USERNAME, PASSWORD);

    /**
     * Creates a PoolableConnectionFactory that will wraps the connection object
     * created by the ConnectionFactory to add object pooling functionality.
     */
    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
    return new PoolingDataSource(connectionPool);
}