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

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

Introduction

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

Prototype

public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool,
        KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly,
        boolean defaultAutoCommit) 

Source Link

Document

Create a new PoolableConnectionFactory.

Usage

From source file:de.xirp.db.XConnectionProvider.java

/**
 * This methos initializes the connection pool using the given
 * URL, user name and password.//w ww  . j a v a  2 s. co  m
 * 
 * @param url
 *            The db URL.
 * @param user
 *            The db user.
 * @param password
 *            The users password.
 */
private void initPool(String url, String user, String password) {

    connectionPool = new GenericObjectPool();
    connectionFactory = new DriverManagerConnectionFactory(url, user, password);
    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null,
            false, true);
    dataSource = new PoolingDataSource(connectionPool);

    dataSource.setAccessToUnderlyingConnectionAllowed(false);

    connectionPool.setMaxActive(MAX_ACTIVE);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
    connectionPool.setMinIdle(MIN_IDLE);
    connectionPool.setTimeBetweenEvictionRunsMillis(EVICTION_TIME);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setTestWhileIdle(false);
    connectionPool.setTestOnReturn(false);

    ds = dataSource;
}

From source file:com.naver.timetable.jdbc.CubridDataManager.java

/**
 * jdbc?  ??  .//from  w  w  w. j a  v  a2s .c  o m
 * @param strDriver
 * @param strDBConn
 * @param strUserID
 * @param strUserPW
 */
public void initDriver() {
    try {
        Class.forName(strDriver);
        Connection objConn = DriverManager.getConnection(strDBConn, strUserID, strUserPW);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    // ?  ? .
    AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setRemoveAbandoned(true);

    AbandonedObjectPool connectionPool = new AbandonedObjectPool(null, abandonedConfig);

    connectionPool.setMaxActive(maxActive);
    connectionPool.setMaxIdle(maxIdle);
    connectionPool.setMinIdle(minIdle);
    connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
    //      connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

    ConnectionFactory driverConnFactory = new DriverManagerConnectionFactory(strDBConn, strUserID, strUserPW);
    PoolableConnectionFactory connFactory = new PoolableConnectionFactory(driverConnFactory, connectionPool,
            null, null, defaultReadOnly, defaultAutoCommit);

    PoolingDataSource pds = new PoolingDataSource(connectionPool);
    dataSource = pds;

    try {
        for (int i = 0; i < initialSize; i++) {
            connectionPool.addObject();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ManualPoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ///*www  . 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  w  w . ja va2s. 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...
    //
    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.jt.dbcp.example.ManualPoolingDataSourceExample.java

public static DataSource setupDataSource(String connectURI) {
    ////from  www .  j a  v  a 2  s . 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, "root", "root");

    //
    // 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:com.jt.dbcp.example.ManualPoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ///*  ww  w .j  a va  2 s .  co  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, "root", "root");

    //
    // 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();/* w w  w  .  ja  v a2  s . c  om*/

    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();
}

From source file:com.iciql.test.IciqlSuite.java

/**
 * Open a new Db object. All connections are cached and re-used to eliminate
 * embedded database startup costs./* w  ww  .  ja  v a 2  s  .  c o  m*/
 * 
 * @return a fresh Db object
 */
public static Db openNewDb() {
    String testUrl = System.getProperty("iciql.url", DEFAULT_TEST_DB.url);
    String testUser = System.getProperty("iciql.user", DEFAULT_TEST_DB.username);
    String testPassword = System.getProperty("iciql.password", DEFAULT_TEST_DB.password);

    Db db = null;
    PoolingDataSource dataSource = dataSources.get(testUrl);
    if (dataSource == null) {
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(testUrl, testUser,
                testPassword);
        GenericObjectPool pool = new GenericObjectPool();
        pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
        PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null, null,
                false, true);
        dataSource = new PoolingDataSource(pool);
        dataSources.put(testUrl, dataSource);
        connectionFactories.put(testUrl, factory);
    }
    db = Db.open(dataSource);

    // drop views
    db.dropView(ProductView.class);
    db.dropView(ProductViewInherited.class);
    db.dropView(ProductViewFromQuery.class);
    db.dropView(ProductViewInheritedComplex.class);

    // drop tables
    db.dropTable(BooleanModel.class);
    db.dropTable(ComplexObject.class);
    db.dropTable(Customer.class);
    db.dropTable(DefaultValuesModel.class);
    db.dropTable(EnumIdModel.class);
    db.dropTable(EnumOrdinalModel.class);
    db.dropTable(EnumStringModel.class);
    db.dropTable(Order.class);
    db.dropTable(PrimitivesModel.class);
    db.dropTable(Product.class);
    db.dropTable(ProductAnnotationOnly.class);
    db.dropTable(ProductInheritedAnnotation.class);
    db.dropTable(ProductMixedAnnotation.class);
    db.dropTable(SupportedTypes.class);
    db.dropTable(JoinTest.UserId.class);
    db.dropTable(JoinTest.UserNote.class);
    db.dropTable(EnumsTest.BadEnums.class);
    db.dropTable(MultipleBoolsModel.class);
    db.dropTable(ProductAnnotationOnlyWithForeignKey.class);
    db.dropTable(CategoryAnnotationOnly.class);

    return db;
}

From source file:com.noelios.restlet.ext.jdbc.JdbcClientHelper.java

/**
 * Creates a connection pool for a given connection configuration.
 * /* w w w  .  ja v a  2  s  .co m*/
 * @param uri
 *            The connection URI.
 * @param properties
 *            The connection properties.
 * @return The new connection pool.
 */
protected static ObjectPool createConnectionPool(String uri, Properties properties) {
    // Create an ObjectPool that will serve as the actual pool of
    // connections
    final ObjectPool result = new GenericObjectPool(null);

    // Create a ConnectionFactory that the pool will use to create
    // Connections
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, properties);

    // Create the PoolableConnectionFactory, which wraps the "real"
    // Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            result, null, null, false, false);

    // To remove warnings
    poolableConnectionFactory.getPool();

    return result;
}

From source file:com.iver.utiles.connections.ConnectionDB.java

/**
 * Registers in the driverpool a new connection
 * /*from  w w w .  j a va  2 s.  c o m*/
 * @param ct
 *            Data connection
 * @param driver
 *            Driver
 * 
 * @throws ConnectionException
 */
public void setupDriver(ConnectionTrans ct) throws ConnectionException {
    String url = ct.getConnBeginning() + "//" + ct.getHost() + ":" + ct.getPort() + "/" + ct.getDb();
    String user = ct.getUser();
    String password = ct.getPassword();
    String name = ct.getHost() + "_" + ct.getName();
    ObjectPool connectionPool = new GenericObjectPool();
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, password);

    try {
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
    } catch (Exception e) {
        throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_pool"), e);
    }

    try {
        Class.forName("org.apache.commons.dbcp.PoolingDriver");
    } catch (ClassNotFoundException e) {
        throw new ConnectionException("Clase : " + "org.apache.commons.dbcp.PoolingDriver", e);
    }

    PoolingDriver driverPool;

    try {
        driverPool = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    } catch (SQLException e) {
        throw new ConnectionException(JDBCManager.getTranslation("fallo_registrar_conexion"), e);
    }

    driverPool.registerPool(name, connectionPool);
    ct.setConnected(true);
}