use of org.apache.commons.pool2.impl.GenericObjectPool in project zeppelin by apache.
the class JDBCInterpreter method createConnectionPool.
private void createConnectionPool(String url, String user, String dbPrefix, Properties properties) throws SQLException, ClassNotFoundException {
LOGGER.info("Creating connection pool for url: {}, user: {}, dbPrefix: {}, properties: {}", url, user, dbPrefix, properties);
/* Remove properties that is not valid properties for presto/trino by checking driver key.
* - Presto: com.facebook.presto.jdbc.PrestoDriver
* - Trino(ex. PrestoSQL): io.trino.jdbc.TrinoDriver / io.prestosql.jdbc.PrestoDriver
*/
String driverClass = properties.getProperty(DRIVER_KEY);
if (driverClass != null && (driverClass.equals("com.facebook.presto.jdbc.PrestoDriver") || driverClass.equals("io.prestosql.jdbc.PrestoDriver") || driverClass.equals("io.trino.jdbc.TrinoDriver"))) {
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(PropertiesUtil.getString(properties, "validationQuery", "show databases"));
ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
this.configConnectionPool((GenericObjectPool) connectionPool, properties);
poolableConnectionFactory.setPool(connectionPool);
Class.forName(driverClass);
PoolingDriver driver = new PoolingDriver();
driver.registerPool(dbPrefix + user, connectionPool);
getJDBCConfiguration(user).saveDBDriverPool(dbPrefix, driver);
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project Openfire by igniterealtime.
the class DefaultConnectionProvider method start.
@Override
public void start() {
try {
Class.forName(driver);
} catch (final ClassNotFoundException e) {
throw new RuntimeException("Unable to find JDBC driver " + driver, e);
}
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, username, password);
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setValidationQuery(testSQL);
poolableConnectionFactory.setValidationQueryTimeout(testTimeout);
poolableConnectionFactory.setMaxConnLifetimeMillis((long) (connectionTimeout * JiveConstants.DAY));
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setTestOnBorrow(testBeforeUse);
poolConfig.setTestOnReturn(testAfterUse);
poolConfig.setMinIdle(minConnections);
if (minConnections > GenericObjectPoolConfig.DEFAULT_MAX_IDLE) {
poolConfig.setMaxIdle(minConnections);
}
poolConfig.setMaxTotal(maxConnections);
poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRuns);
poolConfig.setSoftMinEvictableIdleTimeMillis(minIdleTime);
poolConfig.setMaxWaitMillis(maxWaitTime);
connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project Openfire by igniterealtime.
the class EmbeddedConnectionProvider method start.
@Override
public void start() {
File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
// If the database doesn't exist, create it.
if (!databaseDir.exists()) {
databaseDir.mkdirs();
}
try {
serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
} catch (IOException ioe) {
Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
}
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));
final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(3);
poolConfig.setMaxTotal(25);
final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
poolableConnectionFactory.setPool(connectionPool);
dataSource = new PoolingDataSource<>(connectionPool);
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project cloudstack by apache.
the class TransactionLegacy method getDefaultDataSource.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static DataSource getDefaultDataSource(final String database) {
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
final GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
return new PoolingDataSource(connectionPool);
}
use of org.apache.commons.pool2.impl.GenericObjectPool in project tomcat by apache.
the class PerUserPoolDataSource method registerPool.
private synchronized void registerPool(final String userName, final String password) throws NamingException, SQLException {
final ConnectionPoolDataSource cpds = testCPDS(userName, password);
// Set up the factory we will use (passing the pool associates
// the factory with the pool, so we do not have to do so
// explicitly)
final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeoutDuration(), isRollbackAfterValidation(), userName, password);
factory.setMaxConn(getMaxConnDuration());
// Create an object pool to contain our PooledConnections
final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory);
factory.setPool(pool);
pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
pool.setLifo(getPerUserLifo(userName));
pool.setMaxIdle(getPerUserMaxIdle(userName));
pool.setMaxTotal(getPerUserMaxTotal(userName));
pool.setMaxWait(Duration.ofMillis(getPerUserMaxWaitMillis(userName)));
pool.setMinEvictableIdle(getPerUserMinEvictableIdleDuration(userName));
pool.setMinIdle(getPerUserMinIdle(userName));
pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
pool.setSoftMinEvictableIdle(getPerUserSoftMinEvictableIdleDuration(userName));
pool.setTestOnCreate(getPerUserTestOnCreate(userName));
pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
pool.setTestOnReturn(getPerUserTestOnReturn(userName));
pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
pool.setTimeBetweenEvictionRuns(getPerUserDurationBetweenEvictionRuns(userName));
pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));
final PooledConnectionManager old = managers.put(getPoolKey(userName), factory);
if (old != null) {
throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
}
}
Aggregations