Search in sources :

Example 1 with PoolingDataSource

use of org.apache.commons.dbcp.PoolingDataSource in project hive by apache.

the class TxnHandler method setupJdbcConnectionPool.

private static synchronized void setupJdbcConnectionPool(HiveConf conf) throws SQLException {
    if (connPool != null)
        return;
    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);
        //if we are waiting for connection for 60s, something is really wrong
        //better raise an error than hang forever
        config.setConnectionTimeoutInMs(60000);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        config.setUser(user);
        config.setPassword(passwd);
        connPool = new BoneCPDataSource(config);
        // Enable retries to work around BONECP bug.
        doRetryOnConnPool = true;
    } else if ("dbcp".equals(connectionPooler)) {
        ObjectPool objectPool = new GenericObjectPool();
        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);
        connPool = new PoolingDataSource(objectPool);
    } else if ("hikaricp".equals(connectionPooler)) {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(driverUrl);
        config.setUsername(user);
        config.setPassword(passwd);
        connPool = new HikariDataSource(config);
    } else if ("none".equals(connectionPooler)) {
        LOG.info("Choosing not to pool JDBC connections");
        connPool = new NoPoolConnectionPool(conf);
    } else {
        throw new RuntimeException("Unknown JDBC connection pooling " + connectionPooler);
    }
}
Also used : PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) BoneCPDataSource(com.jolbox.bonecp.BoneCPDataSource) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) HikariConfig(com.zaxxer.hikari.HikariConfig) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) BoneCPConfig(com.jolbox.bonecp.BoneCPConfig) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) ObjectPool(org.apache.commons.pool.ObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory)

Example 2 with PoolingDataSource

use of org.apache.commons.dbcp.PoolingDataSource in project hive by apache.

the class TxnHandler method setupJdbcConnectionPool.

private static synchronized DataSource setupJdbcConnectionPool(Configuration conf, int maxPoolSize, long getConnectionTimeoutMs) throws SQLException {
    String driverUrl = DataSourceProvider.getMetastoreJdbcDriverUrl(conf);
    String user = DataSourceProvider.getMetastoreJdbcUser(conf);
    String passwd = DataSourceProvider.getMetastoreJdbcPasswd(conf);
    String connectionPooler = MetastoreConf.getVar(conf, ConfVars.CONNECTION_POOLING_TYPE).toLowerCase();
    if ("bonecp".equals(connectionPooler)) {
        // Enable retries to work around BONECP bug.
        doRetryOnConnPool = true;
        return new BoneCPDataSourceProvider().create(conf);
    } 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)) {
        return new HikariCPDataSourceProvider().create(conf);
    } 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);
    }
}
Also used : PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) HikariCPDataSourceProvider(org.apache.hadoop.hive.metastore.datasource.HikariCPDataSourceProvider) BoneCPDataSourceProvider(org.apache.hadoop.hive.metastore.datasource.BoneCPDataSourceProvider) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory)

Example 3 with PoolingDataSource

use of org.apache.commons.dbcp.PoolingDataSource in project zm-mailbox by Zimbra.

the class DbPool method getConnection.

public static DbConnection getConnection(Mailbox mbox) throws ServiceException {
    if (!isInitialized()) {
        throw ServiceException.FAILURE("Database connection pool not initialized.", null);
    }
    // -1 == zimbra db and/or initialization where mbox isn't known yet
    Integer mboxId = mbox != null ? mbox.getId() : -1;
    try {
        Db.getInstance().preOpen(mboxId);
        long start = ZimbraPerf.STOPWATCH_DB_CONN.start();
        // If the connection pool is overutilized, warn about potential leaks
        PoolingDataSource pool = getPool();
        checkPoolUsage();
        Connection dbconn = null;
        DbConnection conn = null;
        try {
            dbconn = pool.getConnection();
            if (dbconn.getAutoCommit() != false)
                dbconn.setAutoCommit(false);
            // handling code in BucketBlobStore.newBlobInfo().
            if (Db.supports(Db.Capability.READ_COMMITTED_ISOLATION))
                dbconn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            conn = new DbConnection(dbconn, mboxId);
            Db.getInstance().postOpen(conn);
        } catch (SQLException e) {
            try {
                if (dbconn != null && !dbconn.isClosed())
                    dbconn.close();
            } catch (SQLException e2) {
                ZimbraLog.sqltrace.warn("DB connection close caught exception", e);
            }
            throw ServiceException.FAILURE("getting database connection", e);
        }
        // If we're debugging, update the counter with the current stack trace
        if (ZimbraLog.dbconn.isDebugEnabled()) {
            Throwable t = new Throwable();
            conn.setStackTrace(t);
            String stackTrace = SystemUtil.getStackTrace(t);
            synchronized (sConnectionStackCounter) {
                sConnectionStackCounter.increment(stackTrace);
            }
        }
        if (mbox != null)
            Db.registerDatabaseInterest(conn, mbox);
        ZimbraPerf.STOPWATCH_DB_CONN.stop(start);
        return conn;
    } catch (ServiceException se) {
        // if connection open fails unlock
        Db.getInstance().abortOpen(mboxId);
        throw se;
    }
}
Also used : PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 4 with PoolingDataSource

use of org.apache.commons.dbcp.PoolingDataSource in project zm-mailbox by Zimbra.

the class DbPool method getPool.

/**
 * 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);
    ConnectionFactory cfac = ZimbraConnectionFactory.getConnectionFactory(pconfig);
    boolean defAutoCommit = false, defReadOnly = false;
    new PoolableConnectionFactory(cfac, sConnectionPool, null, null, defReadOnly, defAutoCommit);
    try {
        // derby requires the .newInstance() call
        Class.forName(pconfig.mDriverClassName).newInstance();
        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;
}
Also used : PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) SQLException(java.sql.SQLException) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException)

Example 5 with PoolingDataSource

use of org.apache.commons.dbcp.PoolingDataSource in project JFramework by gugumall.

the class DBCPConnectionProvider method configure.

/*
	 *  (non-Javadoc)
	 * @see j.dao.connection.ConnectionProvider#configure(java.util.Properties)
	 */
public void configure(Properties props) throws Exception {
    String jdbcDriverClass = props.getProperty(Environment.DRIVER);
    String jdbcUrl = props.getProperty(Environment.URL);
    Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);
    if (jdbcDriverClass == null) {
        log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
    } else {
        try {
            Class.forName(jdbcDriverClass);
        } catch (ClassNotFoundException cnfe) {
            String msg = "JDBC Driver class not found: " + jdbcDriverClass;
            log.fatal(msg);
            throw new Exception(msg);
        }
    }
    try {
        // We'll need a ObjectPool that serves as the
        // actual pool of connections.
        connectionPool = new GenericObjectPool(null, PropertiesHelper.getInt(Environment.DBCP_MAXACTIVE, props, 8), PropertiesHelper.getByte(Environment.DBCP_WHENEXHAUSTED, props, (byte) 1), PropertiesHelper.getLong(Environment.DBCP_MAXWAIT, props, -1), PropertiesHelper.getInt(Environment.DBCP_MAXIDLE, props, 8), PropertiesHelper.getBoolean(Environment.DBCP_VALIDATION_ONBORROW, props), PropertiesHelper.getBoolean(Environment.DBCP_VALIDATION_ONRETURN, props));
        // check whether we use prepare statement caching or not
        if (props.getProperty(Environment.DBCP_PS_MAXACTIVE) == null) {
            // log.info("DBCP prepared statement pooling disabled");
            statementPool = null;
        } else {
            // We'll need a KeyedObjectPoolFactory that serves as the
            // actual pool of prepared statements.
            // log.info("DBCP prepared statement pooling enabled");
            statementPool = new GenericKeyedObjectPoolFactory(null, PropertiesHelper.getInt(Environment.DBCP_PS_MAXACTIVE, props, 8), PropertiesHelper.getByte(Environment.DBCP_PS_WHENEXHAUSTED, props, (byte) 1), PropertiesHelper.getLong(Environment.DBCP_PS_MAXWAIT, props, -1), PropertiesHelper.getInt(Environment.DBCP_PS_MAXIDLE, props, 8));
        }
        // Next, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory.
        ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, connectionProps);
        // Now we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        String validationQuery = PropertiesHelper.getString(Environment.DBCP_VALIDATION_QUERY, props, "SELECT 1");
        new PoolableConnectionFactory(connectionFactory, connectionPool, statementPool, validationQuery, false, false);
        // Finally, we create the PoolingDriver itself,
        // passing in the object pool we created.
        ds = new PoolingDataSource(connectionPool);
    } catch (Exception e) {
        log.fatal("could not instantiate DBCP connection pool", e);
        throw new Exception("Could not instantiate DBCP connection pool", e);
    }
    String i = props.getProperty(Environment.ISOLATION);
    if (i == null) {
        isolation = null;
    } else {
        isolation = new Integer(i);
    // log.info("JDBC isolation level: "+ Environment.isolationLevelToString(isolation.intValue()));
    }
}
Also used : DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) ConnectionFactory(org.apache.commons.dbcp.ConnectionFactory) PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp.DriverManagerConnectionFactory) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool) SQLException(java.sql.SQLException) PoolableConnectionFactory(org.apache.commons.dbcp.PoolableConnectionFactory) GenericKeyedObjectPoolFactory(org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory)

Aggregations

PoolingDataSource (org.apache.commons.dbcp.PoolingDataSource)7 ConnectionFactory (org.apache.commons.dbcp.ConnectionFactory)6 PoolableConnectionFactory (org.apache.commons.dbcp.PoolableConnectionFactory)6 GenericObjectPool (org.apache.commons.pool.impl.GenericObjectPool)6 DriverManagerConnectionFactory (org.apache.commons.dbcp.DriverManagerConnectionFactory)5 SQLException (java.sql.SQLException)4 ServiceException (com.zimbra.common.service.ServiceException)2 KeyedObjectPoolFactory (org.apache.commons.pool.KeyedObjectPoolFactory)2 GenericKeyedObjectPoolFactory (org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory)2 BoneCPConfig (com.jolbox.bonecp.BoneCPConfig)1 BoneCPDataSource (com.jolbox.bonecp.BoneCPDataSource)1 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 IOException (java.io.IOException)1 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 NamingException (javax.naming.NamingException)1 AbandonedConfig (org.apache.commons.dbcp.AbandonedConfig)1 AbandonedObjectPool (org.apache.commons.dbcp.AbandonedObjectPool)1 ObjectPool (org.apache.commons.pool.ObjectPool)1