Example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

List of usage examples for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool GenericObjectPool.

Prototype

public GenericObjectPool() 

Source Link

Document

Create a new GenericObjectPool with default properties.

Usage

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public synchronized static void checkGlobalPoolingDataSource(String url, String user, String pass) {
    if (globalDbConPool != null) {
        return;//w w  w . ja  v  a2s .  c o m
    } else {
        LOG.error(
                "#################################################################### init master connetion pool");
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, pass);
        GenericObjectPool connectionPool = new GenericObjectPool();
        connectionPool.setMaxActive(poolActiveSize);
        connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        connectionPool.setMaxWait(10000);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        connectionPool.setTimeBetweenEvictionRunsMillis(30000);
        connectionPool.setMinEvictableIdleTimeMillis(300000);
        connectionPool.setLifo(true);

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

        globalDbConPool = new PoolingDataSource(connectionPool);
        LOG.error(
                "#################################################################### init global connetion pool over");
    }
}

From source file:org.apache.hadoop.hive.metastore.MyXid.java

public synchronized static PoolingDataSource getSegPoolingDataSource(String url, String user, String pass) {
    url = url.toLowerCase();//from ww w  . ja v  a2  s.  c om
    PoolingDataSource pool = SegmentDbConPool.get(url);
    if (pool != null) {
        return pool;
    } else {
        LOG.debug(
                "#################################################################### init global connetion pool:"
                        + url);
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, pass);
        GenericObjectPool connectionPool = new GenericObjectPool();
        connectionPool.setMaxActive(poolActiveSize);
        connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
        connectionPool.setMaxWait(10000);
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        connectionPool.setTimeBetweenEvictionRunsMillis(30000);
        connectionPool.setMinEvictableIdleTimeMillis(300000);
        connectionPool.setLifo(true);

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

        pool = new PoolingDataSource(connectionPool);
        SegmentDbConPool.put(url, pool);

        LOG.debug(
                "#################################################################### init global connetion pool:"
                        + url + " over");
        return pool;
    }
}

From source file:org.apache.hadoop.hive.metastore.txn.TxnHandler.java

private static synchronized DataSource setupJdbcConnectionPool(HiveConf conf, int maxPoolSize,
        long getConnectionTimeoutMs) throws SQLException {
    String driverUrl = HiveConf.getVar(conf, HiveConf.ConfVars.METASTORECONNECTURLKEY);
    String user = getMetastoreJdbcUser(conf);
    String passwd = getMetastoreJdbcPasswd(conf);
    String connectionPooler = conf.getVar(HiveConf.ConfVars.METASTORE_CONNECTION_POOLING_TYPE).toLowerCase();

    if ("bonecp".equals(connectionPooler)) {
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl(driverUrl);/*from  w ww  .ja va 2  s  . co  m*/
        //if we are waiting for connection for a long time, something is really wrong
        //better raise an error than hang forever
        //see DefaultConnectionStrategy.getConnectionInternal()
        config.setConnectionTimeoutInMs(getConnectionTimeoutMs);
        config.setMaxConnectionsPerPartition(maxPoolSize);
        config.setPartitionCount(1);
        config.setUser(user);
        config.setPassword(passwd);
        doRetryOnConnPool = true; // Enable retries to work around BONECP bug.
        return new BoneCPDataSource(config);
    } else if ("dbcp".equals(connectionPooler)) {
        GenericObjectPool objectPool = new GenericObjectPool();
        //https://commons.apache.org/proper/commons-pool/api-1.6/org/apache/commons/pool/impl/GenericObjectPool.html#setMaxActive(int)
        objectPool.setMaxActive(maxPoolSize);
        objectPool.setMaxWait(getConnectionTimeoutMs);
        ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd);
        // This doesn't get used, but it's still necessary, see
        // http://svn.apache.org/viewvc/commons/proper/dbcp/branches/DBCP_1_4_x_BRANCH/doc/ManualPoolingDataSourceExample.java?view=markup
        PoolableConnectionFactory poolConnFactory = new PoolableConnectionFactory(connFactory, objectPool, null,
                null, false, true);
        return new PoolingDataSource(objectPool);
    } else if ("hikaricp".equals(connectionPooler)) {
        HikariConfig config = new HikariConfig();
        config.setMaximumPoolSize(maxPoolSize);
        config.setJdbcUrl(driverUrl);
        config.setUsername(user);
        config.setPassword(passwd);
        //https://github.com/brettwooldridge/HikariCP
        config.setConnectionTimeout(getConnectionTimeoutMs);

        return new HikariDataSource(config);
    } else if ("none".equals(connectionPooler)) {
        LOG.info("Choosing not to pool JDBC connections");
        return new NoPoolConnectionPool(conf);
    } else {
        throw new RuntimeException("Unknown JDBC connection pooling " + connectionPooler);
    }
}

From source file:org.apache.lens.server.util.UtilityMethods.java

public static DataSource getPoolingDataSourceFromConf(Configuration conf) {
    final ConnectionFactory cf = new DriverManagerConnectionFactory(
            conf.get(LensConfConstants.SERVER_DB_JDBC_URL, LensConfConstants.DEFAULT_SERVER_DB_JDBC_URL),
            conf.get(LensConfConstants.SERVER_DB_JDBC_USER, LensConfConstants.DEFAULT_SERVER_DB_USER),
            conf.get(LensConfConstants.SERVER_DB_JDBC_PASS, LensConfConstants.DEFAULT_SERVER_DB_PASS));
    final GenericObjectPool connectionPool = new GenericObjectPool();
    connectionPool.setTestOnBorrow(false);
    connectionPool.setTestOnReturn(false);
    connectionPool.setTestWhileIdle(true);
    new PoolableConnectionFactory(cf, connectionPool, null,
            conf.get(LensConfConstants.SERVER_DB_VALIDATION_QUERY,
                    LensConfConstants.DEFAULT_SERVER_DB_VALIDATION_QUERY),
            false, false).setDefaultAutoCommit(true);
    return new PoolingDataSource(connectionPool);
}

From source file:org.apache.mahout.cf.taste.impl.model.jdbc.ConnectionPoolDataSource.java

public ConnectionPoolDataSource(DataSource underlyingDataSource) {
    Preconditions.checkNotNull(underlyingDataSource);
    ConnectionFactory connectionFactory = new ConfiguringConnectionFactory(underlyingDataSource);
    GenericObjectPool objectPool = new GenericObjectPool();
    objectPool.setTestOnBorrow(false);/* w  ww  .  j av a 2  s  .co  m*/
    objectPool.setTestOnReturn(false);
    objectPool.setTestWhileIdle(true);
    objectPool.setTimeBetweenEvictionRunsMillis(60 * 1000L);
    // Constructor actually sets itself as factory on pool
    new PoolableConnectionFactory(connectionFactory, objectPool, null, "SELECT 1", false, false);
    delegate = new PoolingDataSource(objectPool);
}

From source file:org.apache.ojb.broker.accesslayer.ConnectionFactoryDBCPImpl.java

protected ObjectPool createConnectionPool(GenericObjectPool.Config config, AbandonedConfig ac) {
    final GenericObjectPool connectionPool;
    final boolean doRemoveAbandoned = ac != null && ac.getRemoveAbandoned();

    if (doRemoveAbandoned) {
        connectionPool = new AbandonedObjectPool(null, ac);
    } else {//from  ww  w .j  a  v  a2  s.com
        connectionPool = new GenericObjectPool();
    }
    connectionPool.setMaxActive(config.maxActive);
    connectionPool.setMaxIdle(config.maxIdle);
    connectionPool.setMinIdle(config.minIdle);
    connectionPool.setMaxWait(config.maxWait);
    connectionPool.setTestOnBorrow(config.testOnBorrow);
    connectionPool.setTestOnReturn(config.testOnReturn);
    connectionPool.setTimeBetweenEvictionRunsMillis(config.timeBetweenEvictionRunsMillis);
    connectionPool.setNumTestsPerEvictionRun(config.numTestsPerEvictionRun);
    connectionPool.setMinEvictableIdleTimeMillis(config.minEvictableIdleTimeMillis);
    connectionPool.setTestWhileIdle(config.testWhileIdle);
    return connectionPool;
}

From source file:org.apache.sqoop.repository.JdbcRepositoryProvider.java

private void initializeRepositoryHandler() {
    String jdbcHandlerClassName = repoContext.getHandlerClassName();

    Class<?> handlerClass = ClassUtils.loadClass(jdbcHandlerClassName);

    if (handlerClass == null) {
        throw new SqoopException(RepositoryError.JDBCREPO_0001, jdbcHandlerClassName);
    }/*from   w w  w.ja  v  a 2s.c  o m*/

    try {
        handler = (JdbcRepositoryHandler) handlerClass.newInstance();
    } catch (Exception ex) {
        throw new SqoopException(RepositoryError.JDBCREPO_0001, jdbcHandlerClassName, ex);
    }

    String connectUrl = repoContext.getConnectionUrl();
    if (connectUrl == null || connectUrl.trim().length() == 0) {
        throw new SqoopException(RepositoryError.JDBCREPO_0002);
    }

    String jdbcDriverClassName = repoContext.getDriverClass();
    if (jdbcDriverClassName == null || jdbcDriverClassName.trim().length() == 0) {
        throw new SqoopException(RepositoryError.JDBCREPO_0003);
    }

    // Initialize a datasource
    Class<?> driverClass = ClassUtils.loadClass(jdbcDriverClassName);

    if (driverClass == null) {
        throw new SqoopException(RepositoryError.JDBCREPO_0003, jdbcDriverClassName);
    }

    try {
        driver = (Driver) driverClass.newInstance();
    } catch (Exception ex) {
        throw new SqoopException(RepositoryError.JDBCREPO_0003, jdbcDriverClassName, ex);
    }

    Properties jdbcProps = repoContext.getConnectionProperties();

    ConnectionFactory connFactory = new DriverManagerConnectionFactory(connectUrl, jdbcProps);

    connectionPool = new GenericObjectPool();
    connectionPool.setMaxActive(repoContext.getMaximumConnections());

    statementPool = new GenericKeyedObjectPoolFactory(null);

    // creating the factor automatically wires the connection pool
    new PoolableConnectionFactory(connFactory, connectionPool, statementPool, handler.validationQuery(), false,
            false, repoContext.getTransactionIsolation().getCode());

    dataSource = new PoolingDataSource(connectionPool);
    txFactory = new JdbcRepositoryTransactionFactory(dataSource);

    repoContext.initialize(dataSource, txFactory);

    handler.initialize(repoContext);

    repository = new JdbcRepository(handler, repoContext);

    LOG.info("JdbcRepositoryProvider initialized");
}

From source file:org.apache.wookie.server.Start.java

private static void configureServer() throws Exception {
    // create embedded jetty instance
    logger.info("Configuring Jetty server");
    server = new Server(port);

    // configure embedded jetty to handle wookie web application
    WebAppContext context = new WebAppContext();
    context.setServer(server);/*  www . j  a  va2  s  .  c  o  m*/
    context.setContextPath("/wookie");
    context.setWar("build/webapp/wookie");

    // enable and configure JNDI container resources
    context.setConfigurationClasses(new String[] { "org.mortbay.jetty.webapp.WebInfConfiguration",
            "org.mortbay.jetty.plus.webapp.EnvConfiguration", "org.mortbay.jetty.plus.webapp.Configuration",
            "org.mortbay.jetty.webapp.JettyWebXmlConfiguration",
            "org.mortbay.jetty.webapp.TagLibConfiguration" });
    if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JPA)) {
        logger.info("Configuring JPA persistence manager");

        // setup derby database directory and logging properties
        if (dbType.equals("derby") && dbUri.startsWith("jdbc:derby:")) {
            int dbUriArgsIndex = dbUri.indexOf(";", 11);
            if (dbUriArgsIndex == -1) {
                dbUriArgsIndex = dbUri.length();
            }
            String databasePath = dbUri.substring(11, dbUriArgsIndex);
            int databaseDirIndex = databasePath.lastIndexOf(File.separatorChar);
            if ((databaseDirIndex == -1) && (File.separatorChar != '/')) {
                databaseDirIndex = databasePath.lastIndexOf('/');
            }
            if (databaseDirIndex != -1) {
                String databaseDir = databasePath.substring(0, databaseDirIndex);
                File databaseDirFile = new File(databaseDir);
                if (!databaseDirFile.exists()) {
                    databaseDirFile.mkdirs();
                }
                String derbyLog = databaseDirFile.getAbsolutePath() + File.separator + "derby.log";
                System.setProperty("derby.stream.error.file", derbyLog);
            }
        }

        // Setup a database connection resource using DBCP
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(dbDriver);
        dataSource.setUrl(dbUri);
        dataSource.setUsername(dbUser);
        dataSource.setPassword(dbPassword);
        dataSource.setMaxActive(80);
        dataSource.setMaxIdle(80);
        dataSource.setInitialSize(5);
        dataSource.setMaxOpenPreparedStatements(0);

        // Set up connection pool
        GenericObjectPool pool = new GenericObjectPool();
        // setup factory and pooling DataSource
        DataSourceConnectionFactory factory = new DataSourceConnectionFactory(dataSource);
        @SuppressWarnings("unused")
        PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(factory, pool, null,
                null, false, true);
        DataSource poolingDataSource = new PoolingDataSource(pool);

        new Resource(JPAPersistenceManager.WIDGET_DATABASE_JNDI_DATASOURCE_NAME, poolingDataSource);
    } else if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JCR)) {
        logger.info("Configuring JCR persistence manager");

        // setup repository directory and derby logging properties
        File repositoryDirFile = new File("widgetRepository");
        if (!repositoryDirFile.exists()) {
            repositoryDirFile.mkdirs();
        }
        String derbyLog = repositoryDirFile.getAbsolutePath() + File.separator + "derby.log";
        System.setProperty("derby.stream.error.file", derbyLog);

        // setup Jackrabbit JCR repository JNDI resource
        String repositoryConfig = repositoryDirFile.getAbsolutePath() + File.separator + "repository.xml";
        Repository repository = new TransientRepository(repositoryConfig, repositoryDirFile.getAbsolutePath());
        new Resource(JCRPersistenceManager.WIDGET_REPOSITORY_JNDI_REPOSITORY_NAME, repository);
    }

    // configure embedded jetty web application handler
    server.addHandler(context);

    // configure embedded jetty authentication realm
    HashUserRealm authedRealm = new HashUserRealm("Authentication Required", "etc/jetty-realm.properties");
    server.setUserRealms(new UserRealm[] { authedRealm });

    logger.info("Configured Jetty server");
}

From source file:org.dataconservancy.index.gqmpsql.DatabaseSessionPool.java

private static DataSource setupDataSource(String uri, String user, String pass) {
    if (LOG.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("Creating database session pool: ")
                .append("URL: {} User: {} Password: {}");
        LOG.debug(sb.toString(),//from w  w  w  . j a v a2s  .c o m
                new Object[] { uri, user, (pass != null && pass.trim().length() != 0) ? "****" : pass });
    }
    ConnectionFactory cf = new DriverManagerConnectionFactory(uri, user, pass);

    ObjectPool pool = new GenericObjectPool();
    new PoolableConnectionFactory(cf, pool, null, null, false, false);

    return new PoolingDataSource(pool);
}

From source file:org.idevlab.rjc.ds.PoolableDataSource.java

private synchronized GenericObjectPool createPool() {
    if (closed) {
        throw new RedisException("Data source is closed");
    }/* w  w w.j a  va  2  s.  c o m*/

    if (connectionPool == null) {
        GenericObjectPool gop = new GenericObjectPool();
        gop.setMaxActive(maxActive);
        gop.setMaxIdle(maxIdle);
        gop.setMinIdle(minIdle);
        gop.setMaxWait(maxWait);
        gop.setTestOnBorrow(testOnBorrow);
        gop.setTestOnReturn(testOnReturn);
        gop.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        gop.setTestWhileIdle(testWhileIdle);
        connectionPool = gop;
        createConnectionFactory();
        try {
            for (int i = 0; i < initialSize; i++) {
                connectionPool.addObject();
            }
        } catch (Exception e) {
            throw new RedisException("Error preloading the connection pool", e);
        }
    }

    return connectionPool;
}