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:com.nridge.core.ds.rdbms.SQLConnectionPool.java

private void create(Properties aProperties) throws NSException {
    Logger appLogger = mAppMgr.getLogger(this, "create");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    String driverClassName = aProperties.getProperty("driverClassName");
    try {//  ww  w. ja va 2s .  c  o  m
        Class.forName(driverClassName).getConstructor().newInstance();
    } catch (Exception e) {
        throw new NSException(String.format("%s: %s", driverClassName, e.getMessage()), e);
    }

    String validationQuery = aProperties.getProperty("validationQuery");
    boolean isAutoCommit = StrUtl.stringToBoolean("defaultAutoCommit");

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(aProperties.getProperty("url"),
            aProperties);
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    // When you pass an ObjectPool into the PoolableConnectionFactory, it will automatically
    // register itself as the PoolableObjectFactory for that pool.

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, validationQuery, false, isAutoCommit);
    mDataSource = new PoolingDataSource(connectionPool);

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:esg.node.util.migrate.UserMigrationTool.java

public UserMigrationTool setupSourceResources(String protocol, String host, String port, String database,
        String user, String password) {

    System.out.println("Setting up source resources...");

    String connectURI = protocol + "//" + host + ":" + port + "/" + database; //zoiks
    log.debug("Source Connection URI  = " + connectURI);
    log.debug("Source Connection User = " + user);
    log.debug("Source Connection Password = " + (null == password ? password : "********"));
    this.connectionPool = new GenericObjectPool(null);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, user, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    this.sourceDataSource = new PoolingDataSource(connectionPool);
    this.queryRunner = new QueryRunner(sourceDataSource);
    return this;
}

From source file:com.zimbra.cs.db.DbPool.java

/** Initializes the connection pool. */
private static synchronized PoolingDataSource getPool() {
    if (isShutdown)
        throw new RuntimeException("DbPool permanently shutdown");

    if (sPoolingDataSource != null)
        return sPoolingDataSource;

    PoolConfig pconfig = Db.getInstance().getPoolConfig();
    sConnectionPool = new GenericObjectPool(null, pconfig.mPoolSize, pconfig.whenExhaustedAction, -1,
            pconfig.mPoolSize);/*from  ww w  .j a v a2 s .  com*/
    ConnectionFactory cfac = ZimbraConnectionFactory.getConnectionFactory(pconfig);

    boolean defAutoCommit = false, defReadOnly = false;
    new PoolableConnectionFactory(cfac, sConnectionPool, null, null, defReadOnly, defAutoCommit);

    try {
        Class.forName(pconfig.mDriverClassName).newInstance(); //derby requires the .newInstance() call
        Class.forName("org.apache.commons.dbcp.PoolingDriver");
    } catch (Exception e) {
        ZimbraLog.system.fatal("can't instantiate DB driver/pool class", e);
        System.exit(1);
    }

    try {
        PoolingDataSource pds = new PoolingDataSource(sConnectionPool);
        pds.setAccessToUnderlyingConnectionAllowed(true);

        Db.getInstance().startup(pds, pconfig.mPoolSize);

        sPoolingDataSource = pds;
    } catch (SQLException e) {
        ZimbraLog.system.fatal("can't initialize connection pool", e);
        System.exit(1);
    }

    if (pconfig.mSupportsStatsCallback)
        ZimbraPerf.addStatsCallback(new DbStats());

    return sPoolingDataSource;
}

From source file:com.nridge.core.ds.rdbms.SQLConnectionPool.java

private void create(Properties aProperties, String anAccount, String anPassword) throws NSException {
    Logger appLogger = mAppMgr.getLogger(this, "create");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    String driverClassName = aProperties.getProperty("driverClassName");
    try {//from w w w  .  ja  va2  s.co m
        Class.forName(driverClassName).getConstructor().newInstance();
    } catch (Exception e) {
        throw new NSException(String.format("%s: %s", driverClassName, e.getMessage()), e);
    }

    String validationQuery = aProperties.getProperty("validationQuery");
    boolean isAutoCommit = StrUtl.stringToBoolean("defaultAutoCommit");

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(aProperties.getProperty("url"),
            anAccount, anPassword);
    GenericObjectPool connectionPool = new GenericObjectPool(null);

    // When you pass an ObjectPool into the PoolableConnectionFactory, it will automatically
    // register itself as the PoolableObjectFactory for that pool.

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, validationQuery, false, isAutoCommit);
    mDataSource = new PoolingDataSource(connectionPool);

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:ch.rgw.tools.JdbcLink.java

/**
 * Verbindung zur Datenbank herstellen//from  ww  w.  j  ava2  s.c o  m
 * 
 * TODO return value is always true because exception is thrown on error
 * 
 * @param user
 *            Username, kann null sein
 * @param password
 *            Passwort, kann null sein
 * @return errcode
 * 
 * @throws JdbcLinkException
 */
public boolean connect(String user, String password) {
    Exception cause = null;
    try {
        sUser = user;
        sPwd = password;
        Driver driver = (Driver) Class.forName(sDrv).newInstance();
        verMajor = driver.getMajorVersion();
        verMinor = driver.getMinorVersion();

        log.log(Level.INFO, "Loading database driver " + sDrv);
        log.log(Level.INFO, "Connecting with database " + sConn);

        //
        // First, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        //
        Properties properties = new Properties();
        properties.put("user", user);
        properties.put("password", password);

        ConnectionFactory connectionFactory = new DriverConnectionFactory(driver, sConn, properties);
        //
        // Next we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        //
        connectionPool = new GenericObjectPool<Connection>(null);
        // configure the connection pool
        connectionPool.setMaxActive(32);
        connectionPool.setMinIdle(2);
        connectionPool.setMaxWait(10000);
        connectionPool.setTestOnBorrow(true);

        new PoolableConnectionFactory(connectionFactory, connectionPool, null, VALIDATION_QUERY, false, true);
        dataSource = new PoolingDataSource(connectionPool);

        // test establishing a connection
        Connection conn = dataSource.getConnection();
        conn.close();

        lastErrorCode = CONNECT_SUCCESS;
        lastErrorString = "Connect successful";
        log.log("Connect successful", Log.DEBUGMSG);
        return true;
    } catch (ClassNotFoundException ex) {
        lastErrorCode = CONNECT_CLASSNOTFOUND;
        lastErrorString = "Class not found exception: " + ex.getMessage();
        cause = ex;
    } catch (InstantiationException e) {
        lastErrorCode = CONNECT_UNKNOWN_ERROR;
        lastErrorString = "Instantiation exception: " + e.getMessage();
        cause = e;
    } catch (IllegalAccessException e) {
        lastErrorCode = CONNECT_UNKNOWN_ERROR;
        lastErrorString = "Illegal access exception: " + e.getMessage();
        cause = e;
    } catch (SQLException e) {
        lastErrorCode = CONNECT_UNKNOWN_ERROR;
        lastErrorString = "SQL exception: " + e.getMessage();
        cause = e;
    } catch (IllegalStateException e) {
        lastErrorCode = CONNECT_UNKNOWN_ERROR;
        lastErrorString = "Illegal state exception: " + e.getMessage();
        cause = e;
    }
    throw JdbcLinkExceptionTranslation.translateException("Connect failed: " + lastErrorString, cause);
}

From source file:com.netspective.axiom.connection.JakartaCommonsDbcpConnectionProvider.java

protected DataSource createDataSource(ValueContext vc, String dataSourceId) throws NamingException {

    DataSourceInfo dataSourceInfo = (DataSourceInfo) dataSourcesInfo.get(dataSourceId);

    if (dataSourceInfo == null)
        throw new NamingException("Data Source: '" + dataSourceId
                + "' not defined as a data source for Jakarta Commons DBCP provider.");

    String driverClassName = dataSourceInfo.driverClass.getTextValueOrBlank(vc);
    try {/*w  w w. j a v  a 2  s .  c om*/
        Class.forName(driverClassName);
    } catch (ClassNotFoundException cnfe) {
        log.error("Driver '" + driverClassName + "' not found for name '" + dataSourceId + "'");
        throw new NamingException("Driver '" + driverClassName + "' not found for name '" + dataSourceId + "'");
    }

    if (log.isDebugEnabled()) {
        log.debug("Initializing data source: '" + dataSourceInfo.getName() + "'\n"
                + "                  driver: '" + driverClassName + "'\n" + "                     url: '"
                + dataSourceInfo.url.getTextValueOrBlank(vc) + "'\n" + "                    user: '"
                + dataSourceInfo.user.getTextValueOrBlank(vc) + "'\n" + "                password: '"
                + dataSourceInfo.password.getTextValueOrBlank(vc) + "'");
    }

    ObjectPool connectionPool = new GenericObjectPool(null, dataSourceInfo.getPoolConfig());
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            dataSourceInfo.url.getTextValueOrBlank(vc), dataSourceInfo.user.getTextValueOrBlank(vc),
            dataSourceInfo.password.getTextValueOrBlank(vc));
    try {
        //The reference to this object is not used within this method.  It's constuctor sets a reference of itself
        //in the conectionPool object we pass as a parameter.
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
    } catch (IllegalStateException e) {
        log.error("Trying to reset the pool factory for data source: '" + dataSourceInfo.name
                + "' when the pool objects are already in use, thus the pool is active.");
        return null;
    }

    catch (Exception e) //Generic Exception being caught here because Constructor of PoolableConnectionFactory is declared that way
    {
        log.error(
                "An Exception was encountered when creating the pool factory in the Jakarta Commons DBCP framework.",
                e);
        return null;
    }

    DbcpPoolingDataSource dataSource = new DbcpPoolingDataSource(connectionPool);

    return dataSource;
}

From source file:ca.sqlpower.sqlobject.SQLDatabase.java

synchronized BaseObjectPool getConnectionPool() {
    if (connectionPool == null) {
        Config poolConfig = new GenericObjectPool.Config();
        poolConfig.maxActive = 5;//from w w w. j a  v  a  2s.  com
        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
        connectionPool = new GenericObjectPool(null, poolConfig);
        ConnectionFactory cf = new JDBCDSConnectionFactory(dataSource);
        new PoolableConnectionFactory(cf, connectionPool, null, null, false, true);
    }
    return connectionPool;
}

From source file:com.cloud.utils.db.Transaction.java

private static DataSource getDefaultDataSource(final String database) {
    final GenericObjectPool connectionPool = new GenericObjectPool(null, 5);
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    return new PoolingDataSource(/* connectionPool */poolableConnectionFactory.getPool());
}

From source file:com.cloud.utils.db.TransactionLegacy.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static DataSource getDefaultDataSource(final String database) {
    final GenericObjectPool connectionPool = new GenericObjectPool(null, 5);
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
            "jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);
    return new PoolingDataSource(/* connectionPool */poolableConnectionFactory.getPool());
}

From source file:ojw28.orm.utils.DbConnectionPool.java

private void createPool() throws Exception {
    mPool = new GenericObjectPool();

    Properties props = new Properties();
    props.setProperty("user", "orm");
    props.setProperty("password", "openroommap");
    ConnectionFactory cf = new DriverConnectionFactory(new org.postgresql.Driver(),
            "jdbc:postgresql://localhost:5432/openroommap", props);

    KeyedObjectPoolFactory kopf = new GenericKeyedObjectPoolFactory(null, 10);

    new PoolableConnectionFactory(cf, mPool, kopf, null, false, false);

    for (int i = 0; i < 5; i++) {
        mPool.addObject();/* w w w .  j  a va2s  . c o m*/
    }

    // PoolingDataSource pds = new PoolingDataSource(gPool);
    PoolingDriver pd = new PoolingDriver();
    pd.registerPool("openroommap", mPool);

    for (int i = 0; i < 5; i++) {
        mPool.addObject();
    }

    mLogger.info("Created connection pool");
    mLogger.info("Active\t" + mPool.getNumActive() + "\tIdle\t" + mPool.getNumIdle());
}