org.apache.commons.dbcp2.PoolingDriver Java Examples

The following examples show how to use org.apache.commons.dbcp2.PoolingDriver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JDBCInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void createConnectionPool(String url, String user, String dbPrefix,
    Properties properties) throws SQLException, ClassNotFoundException {

  String driverClass = properties.getProperty(DRIVER_KEY);
  if (driverClass != null && (driverClass.equals("com.facebook.presto.jdbc.PrestoDriver")
          || driverClass.equals("io.prestosql.jdbc.PrestoDriver"))) {
    // Only add valid properties otherwise presto won't work.
    for (String key : properties.stringPropertyNames()) {
      if (!PRESTO_PROPERTIES.contains(key)) {
        properties.remove(key);
      }
    }
  }

  ConnectionFactory connectionFactory =
          new DriverManagerConnectionFactory(url, properties);

  PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
          connectionFactory, null);
  final String maxConnectionLifetime =
      StringUtils.defaultIfEmpty(getProperty("zeppelin.jdbc.maxConnLifetime"), "-1");
  poolableConnectionFactory.setMaxConnLifetimeMillis(Long.parseLong(maxConnectionLifetime));
  poolableConnectionFactory.setValidationQuery("show databases");
  ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);

  poolableConnectionFactory.setPool(connectionPool);
  Class.forName(driverClass);
  PoolingDriver driver = new PoolingDriver();
  driver.registerPool(dbPrefix + user, connectionPool);
  getJDBCConfiguration(user).saveDBDriverPool(dbPrefix, driver);
}
 
Example #2
Source File: PoolingDriverExample.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
public static void printDriverStats() throws Exception {
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    ObjectPool<? extends Connection> connectionPool = driver.getConnectionPool("example");

    System.out.println("NumActive: " + connectionPool.getNumActive());
    System.out.println("NumIdle: " + connectionPool.getNumIdle());
}
 
Example #3
Source File: PoolingDriverConnectionSource.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
private PoolingDriver getPoolingDriver() throws SQLException {
    final PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(URL_PREFIX);
    if (driver == null) {
        getLogger().error("No JDBC driver for '{}'", URL_PREFIX);
    }
    return driver;
}
 
Example #4
Source File: PoolingDriverConnectionSource.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
    try {
        final PoolingDriver driver = getPoolingDriver();
        if (driver != null) {
            getLogger().debug("Driver {} closing DBCP pool '{}'", driver, poolName);
            driver.closePool(poolName);
        }
        return true;
    } catch (final Exception e) {
        getLogger().error("Exception stopping connection source for '{}' → '{}'", getConnectionString(),
                getActualConnectionString(), e);
        return false;
    }
}
 
Example #5
Source File: JDBCUserConfigurations.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public void saveDBDriverPool(String dbPrefix, PoolingDriver driver) throws SQLException {
  poolingDriverMap.put(dbPrefix, driver);
  isSuccessful.put(dbPrefix, false);
}
 
Example #6
Source File: JDBCUserConfigurations.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public PoolingDriver removeDBDriverPool(String key) throws SQLException {
  isSuccessful.remove(key);
  return poolingDriverMap.remove(key);
}
 
Example #7
Source File: JDBCInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private void closeDBPool(String user, String propertyKey) throws SQLException {
  PoolingDriver poolingDriver = getJDBCConfiguration(user).removeDBDriverPool(propertyKey);
  if (poolingDriver != null) {
    poolingDriver.closePool(propertyKey + user);
  }
}
 
Example #8
Source File: PoolingDriverExample.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
public static void setupDriver(String connectURI) throws Exception {
    //
    // First, 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);

    //
    // Next, 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, null);

    //
    // Now 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<PoolableConnection> connectionPool =
        new GenericObjectPool<>(poolableConnectionFactory);
    
    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

    //
    // ...and register our pool with it.
    //
    driver.registerPool("example",connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
}
 
Example #9
Source File: PoolingDriverExample.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
public static void shutdownDriver() throws Exception {
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    driver.closePool("example");
}
 
Example #10
Source File: ConnectionUtils.java    From FROST-Server with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * Set up a connection pool. The driver used in the connection URI should
 * already be loaded using
 * Class.forName("org.apache.commons.dbcp2.PoolingDriver"); After calling
 * this you can use "jdbc:apache:commons:dbcp:FROST-Pool" to connect.
 *
 * @param name The name of the pool to create.
 * @param connectURI The URL of the database to connect to.
 * @param username The username to use when connecting to the database.
 * @param password The password to use when connecting to the database.
 * @throws ClassNotFoundException If the PoolingDriver is not on the
 * classpath.
 * @throws SQLException If the dbcp driver could not be loaded.
 */
public static void setupPoolingDriver(String name, String connectURI, String username, String password) throws ClassNotFoundException, SQLException {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    driver.registerPool(name, connectionPool);
}