/** * Determine the connection synchronization order to use for the given * DataSource. Decreased for every level of nesting that a DataSource * has, checked through the level of DelegatingDataSource nesting. * @param dataSource the DataSource to check * @return the connection synchronization order to use * @see #CONNECTION_SYNCHRONIZATION_ORDER */ private static int getConnectionSynchronizationOrder(DataSource dataSource) { int order = CONNECTION_SYNCHRONIZATION_ORDER; DataSource currDs = dataSource; while (currDs instanceof DelegatingDataSource) { order--; currDs = ((DelegatingDataSource) currDs).getTargetDataSource(); } return order; }
/** * Create a new DelegatingDataSource. * @param targetDataSource the target DataSource */ public DelegatingDataSource(DataSource targetDataSource) { setTargetDataSource(targetDataSource); }
@Test public void shouldDelegateGetConnection() throws Exception { Connection connection = mock(Connection.class); given(delegate.getConnection()).willReturn(connection); assertThat(dataSource.getConnection(), is(connection)); }
@Override public void reload() throws Exception { // switch data source ComboPooledDataSource oldTargetDataSource = (ComboPooledDataSource)proxyDataSource.getTargetDataSource(); ComboPooledDataSource newTargetDataSource = createNewDataSource(); newTargetDataSource.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool proxyDataSource.setTargetDataSource(newTargetDataSource); oldTargetDataSource.close(); } });
private final DelegatingDataSource dgds = new DelegatingDataSource(); dgds.setTargetDataSource(scds);
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); // Determine default auto-commit and transaction isolation // via a Connection from the target DataSource, if possible. if (this.defaultAutoCommit == null || this.defaultTransactionIsolation == null) { try { try (Connection con = obtainTargetDataSource().getConnection()) { checkDefaultConnectionProperties(con); } } catch (SQLException ex) { logger.debug("Could not retrieve default auto-commit and transaction isolation settings", ex); } } }
@Override public void reload() throws Exception { // switch data source BoneCPDataSource oldTargetDataSource = (BoneCPDataSource)proxyDataSource.getTargetDataSource(); BoneCPDataSource newTargetDataSource = createNewDataSource(); newTargetDataSource.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool proxyDataSource.setTargetDataSource(newTargetDataSource); oldTargetDataSource.close(); } });
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); // Determine default auto-commit and transaction isolation // via a Connection from the target DataSource, if possible. if (this.defaultAutoCommit == null || this.defaultTransactionIsolation == null) { try { try (Connection con = obtainTargetDataSource().getConnection()) { checkDefaultConnectionProperties(con); } } catch (SQLException ex) { logger.debug("Could not retrieve default auto-commit and transaction isolation settings", ex); } } }
/** * Obtain the target {@code DataSource} for actual use (never {@code null}). * @since 5.0 */ protected DataSource obtainTargetDataSource() { DataSource dataSource = getTargetDataSource(); Assert.state(dataSource != null, "No 'targetDataSource' set"); return dataSource; }
@Override public void reload() throws Exception { // switch data source DruidDataSource oldTargetDataSource = (DruidDataSource)proxyDataSource.getTargetDataSource(); DruidDataSource newTargetDataSource = createNewDataSource(); newTargetDataSource.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool proxyDataSource.setTargetDataSource(newTargetDataSource); oldTargetDataSource.close(); } });
@Test public void shouldDelegateGetConnectionWithUsernameAndPassword() throws Exception { Connection connection = mock(Connection.class); String username = "username"; String password = "password"; given(delegate.getConnection(username, password)).willReturn(connection); assertThat(dataSource.getConnection(username, password), is(connection)); }
/** * Create a new DelegatingDataSource. * @param targetDataSource the target DataSource */ public DelegatingDataSource(DataSource targetDataSource) { setTargetDataSource(targetDataSource); }
@Override public void afterPropertiesSet() { super.afterPropertiesSet(); // Determine default auto-commit and transaction isolation // via a Connection from the target DataSource, if possible. if (this.defaultAutoCommit == null || this.defaultTransactionIsolation == null) { try { Connection con = getTargetDataSource().getConnection(); try { checkDefaultConnectionProperties(con); } finally { con.close(); } } catch (SQLException ex) { logger.warn("Could not retrieve default auto-commit and transaction isolation settings", ex); } } }
@Override public void afterPropertiesSet() { if (getTargetDataSource() == null) { throw new IllegalArgumentException("Property 'targetDataSource' is required"); } }
protected Connection call(String username, String password, boolean useArgs) throws SQLException { if (useArgs) { return super.getConnection(username, password); } else { return super.getConnection(); } }
/** * Create a new DelegatingDataSource. * @param targetDataSource the target DataSource */ public DelegatingDataSource(DataSource targetDataSource) { setTargetDataSource(targetDataSource); }
private static DataSource getTargetDataSource(DataSource dataSource) { if (dataSource instanceof DelegatingDataSource) { return ((DelegatingDataSource) dataSource).getTargetDataSource(); } return null; }
@Override public Connection getConnection() throws SQLException { if (successfullyConnected) return super.getConnection(); synchronized (this) { Connection connection; try { connection = super.getConnection(); successfullyConnected = true; } catch (SQLException e) { logger.info("Database connection failed. Trying again with url-decoded jdbc url"); DataSource targetDataSource = getTargetDataSource(); if (targetDataSource == null) throw new IllegalStateException("target DataSource should never be null"); BeanWrapper dataSourceWrapper = new BeanWrapperImpl(targetDataSource); String decodedJdbcUrl = decode((String) dataSourceWrapper.getPropertyValue(urlPropertyName)); DataSource urlDecodedConnectionTestDataSource = connectionTestDataSourceFactory.apply(decodedJdbcUrl); urlDecodedConnectionTestDataSource.getConnection(); logger.info("Connection test successful. Continuing with url-decoded jdbc url"); dataSourceWrapper.setPropertyValue(urlPropertyName, decodedJdbcUrl); connection = super.getConnection(); successfullyConnected = true; } return connection; } }
@Override public DataSource resolve(DataSource dataSource) { while (dataSource instanceof DelegatingDataSource) { dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource(); } return dataSource; } }
@Override public Connection getConnection() throws SQLException { if (successfullyConnected) return super.getConnection(); synchronized (this) { Connection connection; try { connection = super.getConnection(); successfullyConnected = true; } catch (SQLException e) { logger.info("Database connection failed. Trying again with url-decoded jdbc url"); DataSource targetDataSource = getTargetDataSource(); if (targetDataSource == null) throw new IllegalStateException("target DataSource should never be null"); BeanWrapper dataSourceWrapper = new BeanWrapperImpl(targetDataSource); String decodedJdbcUrl = decode((String) dataSourceWrapper.getPropertyValue(urlPropertyName)); DataSource urlDecodedConnectionTestDataSource = connectionTestDataSourceFactory.apply(decodedJdbcUrl); urlDecodedConnectionTestDataSource.getConnection(); logger.info("Connection test successful. Continuing with url-decoded jdbc url"); dataSourceWrapper.setPropertyValue(urlPropertyName, decodedJdbcUrl); connection = super.getConnection(); successfullyConnected = true; } return connection; } }