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.peanuts.database.DatabaseFactory.java

/**
 * Sets up Connection Factory and Pool/*from  ww  w  .j a va 2  s .  c  o m*/
 * 
 * @return DataSource configured datasource
 * @throws Exception
 *             if initialization failed
 */
private static DataSource setupDataSource() throws Exception {
    // Create Connection Factory
    ConnectionFactory conFactory = new DriverManagerConnectionFactory(JDBCConfig.JDBC_URL, JDBCConfig.JDBC_USER,
            JDBCConfig.JDBC_PASSWORD);

    // Makes Connection Factory Pool-able (Wrapper for two objects)
    new PoolableConnectionFactory(conFactory, connectionPool, null, null, false, true);

    // Create data source to utilize Factory and Pool
    return new PoolingDataSource(connectionPool);
}

From source file:edu.jhu.pha.vospace.DbPoolServlet.java

@Override
public void init() throws ServletException {
    ServletContext context = this.getServletContext();

    Configuration conf = (Configuration) context.getAttribute("configuration");

    try {/*from  w  ww  . ja v  a2 s.  c o m*/
        Class.forName(conf.getString("db.driver"));
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new ServletException(e);
    }

    GenericObjectPool pool = new GenericObjectPool(null);
    pool.setMinEvictableIdleTimeMillis(6 * 60 * 60 * 1000);
    pool.setTimeBetweenEvictionRunsMillis(30 * 60 * 1000);
    pool.setNumTestsPerEvictionRun(-1);

    DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.getString("db.url"),
            conf.getString("db.login"), conf.getString("db.password"));

    PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, null, "SELECT * FROM mysql.db",
            false, true);
    new PoolingDriver().registerPool("dbPool", pool);
}

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

protected void createPoolableConnectionFactory(String url, String username, String password,
        boolean defaultReadOnly, boolean defaultAutoCommit) {
    ConnectionFactory connectionFactory = new JdbcDriverManagerConnectionFactory(url, username, password);
    new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, defaultReadOnly,
            defaultAutoCommit);//from   w  w w  . j  av  a  2 s. c o m
}

From source file:com.haulmont.yarg.console.PropertiesSqlLoaderFactory.java

protected DataSource setupDataSource(String driver, String connectURI, String username, String password,
        Integer maxActive, Integer maxIdle, Integer maxWait) {
    try {/*from w  w  w.  j  a  va  2  s  .c  om*/
        Class.forName(driver);
        final AbandonedConfig config = new AbandonedConfig();
        config.setLogAbandoned(true);

        AbandonedObjectPool connectionPool = new AbandonedObjectPool(null, config);

        connectionPool.setMaxIdle(maxIdle);
        connectionPool.setMaxActive(maxActive);
        if (maxWait != null) {
            connectionPool.setMaxWait(maxWait);
        }

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

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

        connectionPool.setFactory(poolableConnectionFactory);
        return new PoolingDataSource(connectionPool);
    } catch (ClassNotFoundException e) {
        throw new InitializationException("An error occurred during creation of new datasource object", e);
    }
}

From source file:au.org.paperminer.common.AbstractServlet.java

private boolean testConnectionPool() {
    boolean res = false;
    String dbDriver = getInitParameter("jdbc.driver.class");
    String dbUrl = getInitParameter("db.url");
    String dbUser = getInitParameter("db.user");
    String dbPass = getInitParameter("db.passwd");

    try {//from  www  . ja va2 s  .  c  o m
        m_logger.debug("DB init class=" + dbDriver + ", url=" + dbUrl + ", user=" + dbUser + " pwd="
                + dbPass.substring(0, 3) + "...");
        Class.forName(dbDriver);
        GenericObjectPool connectionPool = new GenericObjectPool(null);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbUrl, dbUser, dbPass);
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
                connectionPool, null, null, false, true);
        m_poolDriver = new PoolingDriver();
        m_poolDriver.registerPool(POOL_NAME, connectionPool);
        //Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:/poolconf");
        TableMaker.checkCreateTables(false);
        res = true;
        m_logger.info("Connection pool started ok");
    } catch (ClassNotFoundException ex) {
        m_logger.error("DB Driver registration failed for " + dbDriver, ex);
    } catch (PaperMinerException ex) {
        m_logger.error("Connection pool check failed", ex);
    }
    return res;
}

From source file:com.ibm.xsp.extlib.relational.jdbc.datasource.dbcp.DbcpPoolDataSource.java

/**
 * /* w w  w.j ava  2  s.co m*/
 */
public DbcpPoolDataSource(final String dataSourceName, final String driverClass, final String url,
        final String username, final String password, final int minIdle, final int maxIdle, final int maxActive,
        final long maxWait) throws PoolException {
    try {
        ds = AccessController.doPrivileged(new PrivilegedExceptionAction<DataSource>() {
            public DataSource run() throws Exception {
                // create a driver connection factory
                driver = JDBCDriverLoader.loadDriver(driverClass);

                Properties properties = new Properties();
                properties.setProperty("user", username); // $NON-NLS-1$
                properties.setProperty("password", (StringUtil.isEmpty(password) ? "" : password)); // $NON-NLS-1$

                ConnectionFactory connectionFactory = new DriverConnectionFactory(driver, url, properties);

                // create the pool
                pool = new GenericObjectPool();
                pool.setMaxActive(maxActive);
                pool.setMaxWait(maxWait);
                pool.setMaxIdle(maxIdle);
                pool.setMinIdle(minIdle);

                // create the pool object factory
                PoolableConnectionFactory factory = new PoolableConnectionFactory(connectionFactory, pool, null,
                        null, false, true);
                pool.setFactory(factory);

                // finally create the datasource
                PoolingDataSource bds = new PoolingDataSource(pool);

                return bds;
            }
        });

    } catch (Exception e) {
        String msg = "Unable to initialize the shared connection pool DataSource"; // $NLX-DbcpPoolDataSource.Unabletoinitializethesharedconnec-1$[["connection pool" is a technical term related to databases]]
        // Note, this resource key is used elsewhere in this plugin
        // "Unable to initialize the shared connection pool DataSource"
        //String msg = com.ibm.xsp.extlib.relational.ResourceHandler.getSpecialAudienceString("DbcpPoolDataSource.Unabletoinitializethesharedconnec");//$NON-NLS-1$
        throw new PoolException(e, msg);
    }
}

From source file:jp.co.acroquest.endosnipe.data.db.H2DataSourceCreator.java

/**
 * {@inheritDoc}/*from   ww  w  .j  av  a 2  s .  c  o  m*/
 */
public DataSource createPoolingDataSource(final String dbname, final boolean connectOnlyExists)
        throws SQLException {
    try {
        Class.forName("org.h2.Driver");
        String uri = createDatabaseURI(dbname, connectOnlyExists);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, USER_NAME, PASSWORD);

        // ????? StackObjectPool ??
        // ????????
        ConnectionManager manager = ConnectionManager.getInstance();
        ObjectPool connectionPool = manager.getConnectionPool(uri);
        if (connectionPool == null) {
            connectionPool = manager.createNewConnectionPool(uri);
        }

        new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
        return new PoolingDataSource(connectionPool);
    } catch (Exception ex) {
        LOGGER.log(EXCEPTION_OCCURED, ex, ex.getMessage());
        throw new SQLException(ex.getMessage());
    }
}

From source file:de.unidue.inf.is.ezdl.dlbackend.database.ApacheDBCPConnectionProvider.java

private PoolingDataSource setupDataSource(String connectURI, String user, String password, boolean autoCommit) {
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.maxActive = 15;/*from w ww. j a v  a 2s  . c  om*/
    config.maxIdle = 10;
    config.minIdle = 3;
    config.maxWait = 1000;

    ObjectPool connectionPool = new GenericObjectPool(null, config);

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

    poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null,
            false, true);
    poolableConnectionFactory.setDefaultAutoCommit(autoCommit);
    PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);

    return poolingDataSource;
}

From source file:ambit2.database.DatasourceFactory.java

public static DataSource setupDataSource(String connectURI) throws AmbitException {
    try {/* w  w w.  ja  v  a2 s. 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);/*from  w  ww  .  j ava 2  s .com*/
    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()));
}