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);
}
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);
}
Aggregations