Java Code Examples for org.springframework.jdbc.datasource.DelegatingDataSource

The following examples show how to use org.springframework.jdbc.datasource.DelegatingDataSource. These examples are extracted from open source projects. 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 Project: cloud-config   Source File: C3P0DataSourceFactoryBean.java    License: 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 Project: cloud-config   Source File: BoneCPDataSourceFactoryBean.java    License: 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 Project: cloud-config   Source File: DruidDataSourceFactoryBean.java    License: 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
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 Project: micronaut-sql   Source File: SpringDataSourceResolver.java    License: 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
@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 Project: cloud-config   Source File: AbstractDataSourceFactoryBean.java    License: 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
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 Project: cloud-config   Source File: C3P0DataSourceFactoryBean.java    License: MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((ComboPooledDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example 10
Source Project: cloud-config   Source File: BoneCPDataSourceFactoryBean.java    License: MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((BoneCPDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example 11
Source Project: cloud-config   Source File: DruidDataSourceFactoryBean.java    License: MIT License 4 votes vote down vote up
@Override
protected void destroyInstance(DataSource instance) throws Exception {
    ((DruidDataSource)((DelegatingDataSource)instance).getTargetDataSource()).close();
}
 
Example 12
private void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertEquals(connectionProp, ReflectionTestUtils.getField(dataSource, "connectionProperties"));
}
 
Example 13
public static void assertConnectionProperties(DataSource dataSource, Properties connectionProp) {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertEquals(connectionProp, ReflectionUtils.getValue(dataSource, "connectionProperties"));
}
 
Example 14
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
public static void assertDataSourceType(DataSource dataSource, Class clazz) throws ClassNotFoundException {
	if (dataSource instanceof DelegatingDataSource) {
		dataSource = ((DelegatingDataSource) dataSource).getTargetDataSource();
	}
	assertThat(dataSource, instanceOf(clazz));
}