Search in sources :

Example 1 with MetricsDataSource

use of io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource in project cdap by cdapio.

the class PostgreSqlStorageProvider method createDataSource.

/**
 * Creates a {@link DataSource} for the sql implementation to use. It optionally loads an external JDBC driver
 * to use with JDBC.
 */
@VisibleForTesting
public static DataSource createDataSource(CConfiguration cConf, SConfiguration sConf, MetricsCollectionService metricsCollectionService) {
    String storageImpl = cConf.get(Constants.Dataset.DATA_STORAGE_IMPLEMENTATION);
    if (!storageImpl.equals(Constants.Dataset.DATA_STORAGE_SQL)) {
        throw new IllegalArgumentException(String.format("The storage implementation is not %s, cannot create the " + "DataSource", Constants.Dataset.DATA_STORAGE_SQL));
    }
    if (cConf.getBoolean(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_EXTERNAL)) {
        loadJDBCDriver(cConf, storageImpl);
    }
    String jdbcUrl = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_CONNECTION_URL);
    if (jdbcUrl == null) {
        throw new IllegalArgumentException("The jdbc connection url is not specified.");
    }
    Properties properties = retrieveJDBCConnectionProperties(cConf, sConf);
    LOG.info("Creating the DataSource with jdbc url: {}", jdbcUrl);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // The GenericObjectPool is thread safe according to the javadoc,
    // the PoolingDataSource will be thread safe as long as the connectin pool is thread-safe
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(cConf.getInt(Constants.Dataset.DATA_STORAGE_SQL_CONNECTION_SIZE));
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    return new MetricsDataSource(dataSource, metricsCollectionService, connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) MetricsDataSource(io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with MetricsDataSource

use of io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource in project cdap by caskdata.

the class PostgreSqlStorageProvider method createDataSource.

/**
 * Creates a {@link DataSource} for the sql implementation to use. It optionally loads an external JDBC driver
 * to use with JDBC.
 */
@VisibleForTesting
public static DataSource createDataSource(CConfiguration cConf, SConfiguration sConf, MetricsCollectionService metricsCollectionService) {
    String storageImpl = cConf.get(Constants.Dataset.DATA_STORAGE_IMPLEMENTATION);
    if (!storageImpl.equals(Constants.Dataset.DATA_STORAGE_SQL)) {
        throw new IllegalArgumentException(String.format("The storage implementation is not %s, cannot create the " + "DataSource", Constants.Dataset.DATA_STORAGE_SQL));
    }
    if (cConf.getBoolean(Constants.Dataset.DATA_STORAGE_SQL_DRIVER_EXTERNAL)) {
        loadJDBCDriver(cConf, storageImpl);
    }
    String jdbcUrl = cConf.get(Constants.Dataset.DATA_STORAGE_SQL_JDBC_CONNECTION_URL);
    if (jdbcUrl == null) {
        throw new IllegalArgumentException("The jdbc connection url is not specified.");
    }
    Properties properties = retrieveJDBCConnectionProperties(cConf, sConf);
    LOG.info("Creating the DataSource with jdbc url: {}", jdbcUrl);
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcUrl, properties);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    // The GenericObjectPool is thread safe according to the javadoc,
    // the PoolingDataSource will be thread safe as long as the connectin pool is thread-safe
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    connectionPool.setMaxTotal(cConf.getInt(Constants.Dataset.DATA_STORAGE_SQL_CONNECTION_SIZE));
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
    return new MetricsDataSource(dataSource, metricsCollectionService, connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) MetricsDataSource(io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) Properties(java.util.Properties) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 MetricsDataSource (io.cdap.cdap.spi.data.sql.jdbc.MetricsDataSource)2 Properties (java.util.Properties)2 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)2 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)2 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)2 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)2 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)2 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)2