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

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

Introduction

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

Prototype

public DriverManagerConnectionFactory(String connectUri, Properties props) 

Source Link

Document

Constructor for DriverManagerConnectionFactory.

Usage

From source file:com.claresco.tinman.servlet.ConnectionPooling.java

/**
 * Constructor //from w ww .  ja va  2  s  .c om
 *
 * Params:
 *
 *
 */
public ConnectionPooling(String connectionURL, String userName, String password, String driverName)
        throws ClassNotFoundException, SQLException {
    Class.forName(driverName);

    Properties props = new Properties();
    props.setProperty("user", userName);
    props.setProperty("password", password);

    ObjectPool connectionPool = new GenericObjectPool(null);

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURL, props);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(myPoolingDriverName);
    driver.registerPool(myPoolName, connectionPool);
}

From source file:iudex.da.DataSourceFactory.java

public DataSource create(Map<String, String> params) {
    String className = params.remove("dsf.driver.class");
    if (className != null) {
        try {//from  w w  w .j a  v a 2 s .co  m
            Class<?> driverClass = Class.forName(className);
            assert (driverClass != null);
        } catch (ClassNotFoundException x) {
            throw new RuntimeException(x);
        }
    }
    setLogWriter();

    String uri = params.remove("dsf.uri");

    Properties props = new Properties();
    for (Map.Entry<String, String> e : params.entrySet()) {
        props.setProperty(e.getKey(), e.getValue());
    }
    DriverManagerConnectionFactory conFactory = new DriverManagerConnectionFactory(uri, props);

    ObjectPool conPool = new GenericObjectPool(null);
    //min, max, etc. connections

    // Sets self on conPool
    new PoolableConnectionFactory(conFactory, conPool, null, //stmtPoolFactory
            null, false, true);

    return new PoolingDataSource(conPool);
}

From source file:ambit2.database.DatasourceFactory.java

public static DataSource setupDataSource(String connectURI) throws AmbitException {
    try {// ww w .ja v  a2s .  c  o m
        Class.forName("com.mysql.jdbc.Driver");
        //
        // 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.
        //
        ObjectPool connectionPool = new GenericObjectPool(null);

        //
        // Next, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
        // using the connect string passed in the command line
        // arguments.
        //
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

        //
        // Now we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        //
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);

        //
        // Finally, we create the PoolingDriver itself,
        // passing in the object pool we created.
        //
        PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
        return dataSource;
    } catch (Exception x) {
        throw new AmbitException(x);
    }
}

From source file:com.panet.imeta.core.database.ConnectionPoolUtil.java

private static void createPool(DatabaseMeta databaseMeta, String partitionId, int initialSize, int maximumSize)
        throws KettleDatabaseException {
    LogWriter.getInstance().logBasic(databaseMeta.toString(),
            Messages.getString("Database.CreatingConnectionPool", databaseMeta.getName()));
    GenericObjectPool gpool = new GenericObjectPool();

    gpool.setMaxIdle(-1);//ww w .  ja  v  a 2s . c  o  m
    gpool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    gpool.setMaxActive(maximumSize);

    String clazz = databaseMeta.getDriverClass();
    try {
        Class.forName(clazz).newInstance();
    } catch (Exception e) {
        throw new KettleDatabaseException(Messages.getString(
                "Database.UnableToLoadConnectionPoolDriver.Exception", databaseMeta.getName(), clazz), e);
    }

    String url;
    String userName;
    String password;

    try {
        url = databaseMeta.environmentSubstitute(databaseMeta.getURL(partitionId));
        userName = databaseMeta.environmentSubstitute(databaseMeta.getUsername());
        password = databaseMeta.environmentSubstitute(databaseMeta.getPassword());
    } catch (RuntimeException e) {
        url = databaseMeta.getURL(partitionId);
        userName = databaseMeta.getUsername();
        password = databaseMeta.getPassword();
    }

    // Get the list of pool properties
    Properties originalProperties = databaseMeta.getConnectionPoolingProperties();
    //Add user/pass
    originalProperties.setProperty("user", Const.NVL(userName, ""));
    originalProperties.setProperty("password", Const.NVL(password, ""));

    // Now, replace the environment variables in there...
    Properties properties = new Properties();
    Iterator<Object> iterator = originalProperties.keySet().iterator();
    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        String value = originalProperties.getProperty(key);
        properties.put(key, databaseMeta.environmentSubstitute(value));
    }

    // Create factory using these properties.
    //
    ConnectionFactory cf = new DriverManagerConnectionFactory(url, properties);

    new PoolableConnectionFactory(cf, gpool, null, null, false, false);

    for (int i = 0; i < initialSize; i++) {
        try {
            gpool.addObject();
        } catch (Exception e) {
            throw new KettleDatabaseException(
                    Messages.getString("Database.UnableToPreLoadConnectionToConnectionPool.Exception"), e);
        }
    }

    pd.registerPool(databaseMeta.getName(), gpool);

    LogWriter.getInstance().logBasic(databaseMeta.toString(),
            Messages.getString("Database.CreatedConnectionPool", databaseMeta.getName()));
}

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  w w w  . jav a2s  .com
        // 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:lineage2.commons.dbcp.BasicDataSource.java

/**
 * Constructor for BasicDataSource.// ww  w. j a  va2s .c om
 * @param driver String
 * @param connectURI String
 * @param uname String
 * @param passwd String
 * @param maxActive int
 * @param maxIdle int
 * @param idleTimeOut int
 * @param idleTestPeriod int
 * @param poolPreparedStatements boolean
 */
public BasicDataSource(String driver, String connectURI, String uname, String passwd, int maxActive,
        int maxIdle, int idleTimeOut, int idleTestPeriod, boolean poolPreparedStatements) {
    GenericObjectPool<?> connectionPool = new GenericObjectPool<>(null);
    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(1);
    connectionPool.setMaxWait(-1L);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestWhileIdle(true);
    connectionPool.setTimeBetweenEvictionRunsMillis(idleTestPeriod * 1000L);
    connectionPool.setNumTestsPerEvictionRun(maxActive);
    connectionPool.setMinEvictableIdleTimeMillis(idleTimeOut * 1000L);
    GenericKeyedObjectPoolFactory<?, ?> statementPoolFactory = null;
    if (poolPreparedStatements) {
        statementPoolFactory = new GenericKeyedObjectPoolFactory<>(null, -1,
                GenericObjectPool.WHEN_EXHAUSTED_FAIL, 0L, 1, GenericKeyedObjectPool.DEFAULT_MAX_TOTAL);
    }
    Properties connectionProperties = new Properties();
    connectionProperties.put("user", uname);
    connectionProperties.put("password", passwd);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, connectionProperties);
    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, statementPoolFactory, "SELECT 1", false, true);
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
    _connectionPool = connectionPool;
    _source = dataSource;
}

From source file:mondrian.rolap.RolapConnectionPool.java

public synchronized DataSource getDriverManagerPoolingDataSource(String jdbcConnectString,
        Properties jdbcProperties) {
    // First look for a data source with identical specification. This in
    // turn helps us to use the cache of Dialect objects.

    // Need to include user name to define the pool key as some DBMSs
    // like Oracle don't include schemas in the JDBC URL - instead the
    // user drives the schema. This makes JDBC pools act like JNDI pools,
    // with, in effect, a pool per DB user.

    List<Object> key = Arrays.<Object>asList("DriverManagerPoolingDataSource", jdbcConnectString,
            jdbcProperties);/* w w w. j a  va2s . c o  m*/
    DataSource dataSource = dataSourceMap.get(key);
    if (dataSource != null) {
        return dataSource;
    }

    // use the DriverManagerConnectionFactory to create connections
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcConnectString, jdbcProperties);

    try {
        String propertyString = jdbcProperties.toString();
        dataSource = getPoolingDataSource(jdbcConnectString + propertyString, connectionFactory);
    } catch (Throwable e) {
        throw Util.newInternal(e, "Error while creating connection pool (with URI " + jdbcConnectString + ")");
    }
    dataSourceMap.put(key, dataSource);
    return dataSource;
}

From source file:ManualPoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ///*from  ww  w  .j a  v a 2  s  .c o m*/
    // 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.
    //
    ObjectPool connectionPool = new GenericObjectPool(null);

    //
    // Next, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource dataSource = new PoolingDataSource(connectionPool);

    return dataSource;
}

From source file:ManualPoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ////from   w ww  .  j  av a 2s  .c  om
    // 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.
    //
    ObjectPool connectionPool = new GenericObjectPool(null);

    //
    // Next, we'll create a ConnectionFactory that the
    // pool will use to create Connections.
    // We'll use the DriverManagerConnectionFactory,
    // using the connect string passed in the command line
    // arguments.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // Now we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

    //
    // ...and register our pool with it.
    //
    driver.registerPool("example", connectionPool);

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

From source file:com.toolsverse.etl.sql.connection.PooledAliasConnectionProvider.java

@Override
protected Connection createConnection(Alias alias) throws Exception {
    java.sql.Driver driver = (java.sql.Driver) Class.forName(alias.getJdbcDriverClass()).newInstance();

    DriverManager.registerDriver(driver);

    org.apache.commons.dbcp.ConnectionFactory connectionFactory = null;

    Properties props = Utils.getProperties(alias.getParams());

    String userId = alias.getUserId();
    String password = alias.getPassword();

    String url = alias.getUrl();//from w  w  w.  ja  v  a  2  s  .c o  m

    if (props.size() > 0) {
        if (!Utils.isNothing(userId)) {
            props.put("user", userId);
            if (!Utils.isNothing(password))
                props.put("password", password);
        }

        connectionFactory = new DriverManagerConnectionFactory(url, props);
    } else
        connectionFactory = new DriverManagerConnectionFactory(url, userId, password);

    ObjectPool connectionPool = new GenericObjectPool(null, _config);

    @SuppressWarnings("unused")
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);

    return poolingDataSource.getConnection();
}