org.springframework.jdbc.datasource.DelegatingDataSource Java Examples

The following examples show how to use org.springframework.jdbc.datasource.DelegatingDataSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: C3P0DataSourceFactoryBean.java    From cloud-config with MIT License 6 votes vote down vote up
@Override
protected DataSource createInstance() throws Exception {
    Preconditions.checkNotNull(config, "config cannot be empty");
    final ComboPooledDataSource targetDataSource = createNewDataSource();
    final DelegatingDataSource proxyDataSource = createProxyDataSource(targetDataSource);
    config.setReloadCallback(new ReloadCallback() {
        @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();
        }
    });
    return proxyDataSource;
}
 
Example #2
Source File: BoneCPDataSourceFactoryBean.java    From cloud-config with MIT License 6 votes vote down vote up
@Override
protected DataSource createInstance() throws Exception {
    Preconditions.checkNotNull(config, "config cannot be empty");
    final BoneCPDataSource targetDataSource = createNewDataSource();
    final DelegatingDataSource proxyDataSource = createProxyDataSource(targetDataSource);
    config.setReloadCallback(new ReloadCallback() {
        @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();
        }
    });
    return proxyDataSource;
}
 
Example #3
Source File: DruidDataSourceFactoryBean.java    From cloud-config with MIT License 6 votes vote down vote up
@Override
protected DataSource createInstance() throws Exception {
    Preconditions.checkNotNull(config, "config cannot be empty");
    final DruidDataSource targetDataSource = createNewDataSource();
    final DelegatingDataSource proxyDataSource = createProxyDataSource(targetDataSource);
    config.setReloadCallback(new ReloadCallback() {
        @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();
        }
    });
    return proxyDataSource;
}
 
Example #4
Source File: AbstractDataSourceCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 6 votes vote down vote up
private void assertDataSourceProperties(RelationalServiceInfo relationalServiceInfo, DataSource dataSource) {
	assertNotNull(dataSource);

	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}

	assertNotNull(dataSource);

	assertEquals(getDriverName(), ReflectionUtils.getValue(dataSource, "driverClassName"));
	assertEquals(relationalServiceInfo.getJdbcUrl(), ReflectionUtils.getValue(dataSource, "url"));

	Object testOnBorrow = ReflectionUtils.getValue(dataSource, "testOnBorrow");
	assertNotNull(testOnBorrow);
	assertTrue(Boolean.valueOf(testOnBorrow.toString()));

	Object validationQuery = ReflectionUtils.getValue(dataSource, "validationQuery");
	assertNotNull(validationQuery);
	assertTrue(validationQuery.toString().startsWith(getValidationQueryStart()));
}
 
Example #5
Source File: SpringDataSourceResolver.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
@Override
public DataSource resolve(DataSource dataSource) {
    while (dataSource instanceof DelegatingDataSource) {
        dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
    }
    return dataSource;
}
 
Example #6
Source File: ProcessStepsConfiguration.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
@Inject
@Bean
public MBeanExporter jmxExporter(Metrics metrics, DataSource dataSource) {
    MBeanExporter mBeanExporter = new MBeanExporter();
    Map<String, Object> beans = new HashMap<>();
    beans.put(METRICS_BEAN, metrics);
    if (dataSource instanceof DelegatingDataSource) {
        dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
    }
    beans.put(DATASOURCE_BEAN, dataSource);
    mBeanExporter.setBeans(beans);
    return mBeanExporter;
}
 
Example #7
Source File: AbstractDataSourceFactoryBean.java    From cloud-config with MIT License 5 votes vote down vote up
protected DelegatingDataSource createProxyDataSource(DataSource targetDataSource) {
    return new DelegatingDataSource(targetDataSource) {
        @Override
        public String toString() {
            if(getTargetDataSource()!=null) {
                return getTargetDataSource().toString();
            } else {
                return "_NULL_DATA_SOURCE_";
            }
        }
    };
}
 
Example #8
Source File: DataSourceCloudConfigTestHelper.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
public static void assertPoolProperties(DataSource dataSource, int maxActive, int minIdle, long maxWait) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
    assertCommonsPoolProperties(dataSource, maxActive, minIdle, maxWait);

}
 
Example #9
Source File: C3P0DataSourceFactoryBean.java    From cloud-config with MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((ComboPooledDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example #10
Source File: BoneCPDataSourceFactoryBean.java    From cloud-config with MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((BoneCPDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example #11
Source File: DruidDataSourceFactoryBean.java    From cloud-config with MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((DruidDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example #12
Source File: AbstractDataSourceCreatorTest.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
private void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertEquals(connectionProp, ReflectionTestUtils.getField(dataSource, "connectionProperties"));
}
 
Example #13
Source File: DataSourceCloudConfigTestHelper.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public static void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertEquals(connectionProp, ReflectionUtils.getValue(dataSource, "connectionProperties"));
}
 
Example #14
Source File: DataSourceCloudConfigTestHelper.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public static void assertConnectionProperty(DataSource dataSource, String key, Object value) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertEquals(value, ReflectionUtils.getValue(dataSource, key));
}
 
Example #15
Source File: DataSourceCloudConfigTestHelper.java    From spring-cloud-connectors with Apache License 2.0 4 votes vote down vote up
public static void assertDataSourceType(DataSource dataSource, Class clazz) throws ClassNotFoundException {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertThat(dataSource, instanceOf(clazz));
}