public static Connection getConnection( LogChannelInterface log, DatabaseMeta dbMeta, String partitionId, int initialSize, int maximumSize ) throws Exception { lock.lock(); try { if ( !isDataSourceRegistered( dbMeta, partitionId ) ) { addPoolableDataSource( log, dbMeta, partitionId, initialSize, maximumSize ); } } finally { lock.unlock(); } BasicDataSource ds = dataSources.get( getDataSourceName( dbMeta, partitionId ) ); return ds.getConnection(); }
public static Connection getConnection( LogChannelInterface log, DatabaseMeta dbMeta, String partitionId ) throws Exception { return getConnection( log, dbMeta, partitionId, dbMeta.getInitialPoolSize(), dbMeta.getMaximumPoolSize() ); }
@VisibleForTesting static void configureDataSource( BasicDataSource ds, DatabaseMeta databaseMeta, String partitionId, int initialSize, int maximumSize ) throws KettleDatabaseException { // substitute variables and populate pool properties; add credentials Properties connectionPoolProperties = new Properties( databaseMeta.getConnectionPoolingProperties() ); connectionPoolProperties = environmentSubstitute( connectionPoolProperties, databaseMeta ); setPoolProperties( ds, connectionPoolProperties, initialSize, maximumSize ); setCredentials( ds, databaseMeta, partitionId ); // add url/driver class String url = databaseMeta.environmentSubstitute( databaseMeta.getURL( partitionId ) ); ds.setUrl( url ); String clazz = databaseMeta.getDriverClass(); if ( databaseMeta.getDatabaseInterface() != null ) { ds.setDriverClassLoader( databaseMeta.getDatabaseInterface().getClass().getClassLoader() ); } ds.setDriverClassName( clazz ); }
/** * This methods adds a new data source to cache * * @param log * @param databaseMeta * @param partitionId * @param initialSize * @param maximumSize * @throws KettleDatabaseException */ private static void addPoolableDataSource( LogChannelInterface log, DatabaseMeta databaseMeta, String partitionId, int initialSize, int maximumSize ) throws KettleDatabaseException { if ( log.isBasic() ) { log.logBasic( BaseMessages.getString( PKG, "Database.CreatingConnectionPool", databaseMeta.getName() ) ); } BasicDataSource ds = new BasicDataSource(); configureDataSource( ds, databaseMeta, partitionId, initialSize, maximumSize ); // check if datasource is valid testDataSource( ds ); // register data source dataSources.put( getDataSourceName( databaseMeta, partitionId ), ds ); if ( log.isBasic() ) { log.logBasic( BaseMessages.getString( PKG, "Database.CreatedConnectionPool", databaseMeta.getName() ) ); } }
private static boolean isDataSourceRegistered( DatabaseMeta dbMeta, String partitionId ) throws KettleDatabaseException { try { String name = getDataSourceName( dbMeta, partitionId ); return dataSources.containsKey( name ); } catch ( Exception e ) { throw new KettleDatabaseException( BaseMessages.getString( PKG, "Database.UnableToCheckIfConnectionPoolExists.Exception" ), e ); } }
this.connection = ConnectionPoolUtil.getConnection( log, databaseMeta, partitionId );