List of usage examples for org.apache.commons.dbcp PoolableConnectionFactory PoolableConnectionFactory
public PoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool, KeyedObjectPoolFactory stmtPoolFactory, String validationQuery, boolean defaultReadOnly, boolean defaultAutoCommit)
From source file:orca.util.db.MySqlPool.java
public static synchronized void registerPool(DriverManagerConnectionFactory factory, String joclFileLocation, String poolName) throws Exception { if (factory == null) { throw new IllegalArgumentException("factory cannot be null"); }// w w w .java 2s. co m if (poolName == null) { throw new IllegalArgumentException("poolName cannot be null"); } // register the pool only if this is a new pool boolean exists = false; String[] pools = driver.getPoolNames(); for (int i = 0; i < pools.length; i++) { if (pools[i].equals(poolName)) { exists = true; break; } } if (!exists) { GenericObjectPool connectionPool = createConnectionPool(joclFileLocation); PoolableConnectionFactory pcf = new PoolableConnectionFactory(factory, connectionPool, null, null, false, true); driver.registerPool(poolName, pcf.getPool()); } }
From source file:org.aitools.programd.Core.java
/** * Returns a database connection backed by a pooling driver. * This is initialized lazily, since some people may not be * using any database-based features.//ww w . j a v a2 s. c o m * * @return a dbmanager */ public Connection getDBConnection() { if (this._connectionPool == null) { try { Class.forName(this._settings.getDatabaseDriver()); } catch (ClassNotFoundException e) { throw new UserError("Could not find your database driver.", e); } this._connectionPool = new GenericObjectPool(); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( this._settings.getDatabaseURL(), this._settings.getDatabaseUsername(), this._settings.getDatabasePassword()); new PoolableConnectionFactory(connectionFactory, this._connectionPool, null, null, false, true); PoolingDriver driver = new PoolingDriver(); driver.registerPool("programd", this._connectionPool); } try { return DriverManager.getConnection("jdbc:apache:commons:dbcp:programd"); } catch (SQLException e) { this._logger.error("SQL exception when getting a db connection.", e); return null; } }
From source file:org.aitools.util.db.DBConnectionManager.java
/** * Create a new database connection manager using the given driver (classname) * and database URI (DBMS-specific)./*from ww w. j ava 2s . co m*/ * * @param logger * @param driver * @param uri * @param username * @param password * @param minIdle * @param maxActive */ public DBConnectionManager(String driver, String uri, String username, String password, int minIdle, int maxActive) { Classes.verifyAvailable(driver, "database driver"); GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setMinIdle(minIdle); connectionPool.setMaxActive(maxActive); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); this._dataSource = new PoolingDataSource(connectionPool); // Was using DdlUtils here, but it did not correctly work for all column properties. //this.checkDBSchema(); }
From source file:org.apache.avalon.dbcp.DbcpConnectionManager.java
/** * //ww w . j a v a 2 s . co m * @param connFactory * @param pool * @param datasource * @return * @throws ConfigurationException */ private PoolableConnectionFactory createPoolableConnectionFactory(ConnectionFactory connFactory, ObjectPool pool, Configuration datasource) throws ConfigurationException { PoolableConnectionFactory factory = null; String query = null; if (datasource.getChild("validation-query", false) != null) { query = datasource.getChild("validation-query").getValue(); } boolean readonly = datasource.getChild("read-only").getValueAsBoolean(false); boolean autocommit = datasource.getChild("auto-commit").getValueAsBoolean(true); factory = new PoolableConnectionFactory(connFactory, pool, null, query, readonly, autocommit); return factory; }
From source file:org.apache.hadoop.hive.metastore.datasource.DbCPDataSourceProvider.java
@Override public DataSource create(Configuration hdpConfig) throws SQLException { LOG.debug("Creating dbcp connection pool for the MetaStore"); String driverUrl = DataSourceProvider.getMetastoreJdbcDriverUrl(hdpConfig); String user = DataSourceProvider.getMetastoreJdbcUser(hdpConfig); String passwd = DataSourceProvider.getMetastoreJdbcPasswd(hdpConfig); int maxPoolSize = hdpConfig.getInt(MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getVarname(), ((Long) MetastoreConf.ConfVars.CONNECTION_POOLING_MAX_CONNECTIONS.getDefaultVal()).intValue()); long connectionTimeout = hdpConfig.getLong(CONNECTION_TIMEOUT_PROPERTY, 30000L); int connectionMaxIlde = hdpConfig.getInt(CONNECTION_MAX_IDLE_PROPERTY, GenericObjectPool.DEFAULT_MAX_IDLE); int connectionMinIlde = hdpConfig.getInt(CONNECTION_MIN_IDLE_PROPERTY, GenericObjectPool.DEFAULT_MIN_IDLE); boolean testOnBorrow = hdpConfig.getBoolean(CONNECTION_TEST_BORROW_PROPERTY, GenericObjectPool.DEFAULT_TEST_ON_BORROW); long evictionTimeMillis = hdpConfig.getLong(CONNECTION_MIN_EVICT_MILLIS_PROPERTY, GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); boolean testWhileIdle = hdpConfig.getBoolean(CONNECTION_TEST_IDLEPROPERTY, GenericObjectPool.DEFAULT_TEST_WHILE_IDLE); long timeBetweenEvictionRuns = hdpConfig.getLong(CONNECTION_TIME_BETWEEN_EVICTION_RUNS_MILLIS, GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); int numTestsPerEvictionRun = hdpConfig.getInt(CONNECTION_NUM_TESTS_PER_EVICTION_RUN, GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN); boolean testOnReturn = hdpConfig.getBoolean(CONNECTION_TEST_ON_RETURN, GenericObjectPool.DEFAULT_TEST_ON_RETURN); long softMinEvictableIdleTimeMillis = hdpConfig.getLong(CONNECTION_SOFT_MIN_EVICTABLE_IDLE_TIME, GenericObjectPool.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS); boolean lifo = hdpConfig.getBoolean(CONNECTION_LIFO, GenericObjectPool.DEFAULT_LIFO); GenericObjectPool objectPool = new GenericObjectPool(); objectPool.setMaxActive(maxPoolSize); objectPool.setMaxWait(connectionTimeout); objectPool.setMaxIdle(connectionMaxIlde); objectPool.setMinIdle(connectionMinIlde); objectPool.setTestOnBorrow(testOnBorrow); objectPool.setTestWhileIdle(testWhileIdle); objectPool.setMinEvictableIdleTimeMillis(evictionTimeMillis); objectPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns); objectPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun); objectPool.setTestOnReturn(testOnReturn); objectPool.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis); objectPool.setLifo(lifo);/*from w w w. j a va2 s. c o m*/ ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, user, passwd); // This doesn't get used, but it's still necessary, see // https://git1-us-west.apache.org/repos/asf?p=commons-dbcp.git;a=blob;f=doc/ManualPoolingDataSourceExample.java; // h=f45af2b8481f030b27364e505984c0eef4f35cdb;hb=refs/heads/DBCP_1_5_x_BRANCH new PoolableConnectionFactory(connFactory, objectPool, null, null, false, true); return new PoolingDataSource(objectPool); }
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;//from www .j ava 2 s .c om } 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();// www .j av a2s .co m 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 www . j a v a2 s . c o 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.jcs.auxiliary.disk.jdbc.JDBCDiskCachePoolAccess.java
/** * @param connectURI/*from w w w. j av a2s .co m*/ * @param userName * @param password * @param maxActive max connetions * @throws Exception */ public void setupDriver(String connectURI, String userName, String password, int maxActive) throws Exception { // 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, maxActive); // TODO make configurable // By dfault the size is 8!!!!!!! ((GenericObjectPool) connectionPool).setMaxIdle(-1); // 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. // Properties props = new Properties(); // props.setProperty( "user", userName ); // props.setProperty( "password", password ); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, userName, password); // 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(DRIVER_NAME); // ...and register our pool with it. driver.registerPool(this.getPoolName(), connectionPool); // Now we can just use the connect string // "jdbc:apache:commons:dbcp:jcs" // to access our pool of Connections. }
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); }