RefreshableDataSource implements DataSource, DataSourceConfigureChangeListener { @Override public synchronized void configChanged(DataSourceConfigureChangeEvent event) throws SQLException { String name = event.getName(); DataSourceConfigure newConfigure = event.getNewDataSourceConfigure(); SingleDataSource oldDataSource = dataSourceReference.getAndSet(newConfigure == null ? null : new SingleDataSource(name, newConfigure)); close(oldDataSource); } RefreshableDataSource(String name, DataSourceConfigure config); @Override synchronized void configChanged(DataSourceConfigureChangeEvent event); @Override Connection getConnection(); @Override Connection getConnection(String paramString1, String paramString2); @Override PrintWriter getLogWriter(); @Override int getLoginTimeout(); @Override void setLogWriter(PrintWriter paramPrintWriter); @Override void setLoginTimeout(int paramInt); @Override java.util.logging.Logger getParentLogger(); @Override T unwrap(Class<T> iface); @Override boolean isWrapperFor(Class<?> iface);  }
@Test public void test() throws SQLException, IllegalAccessException { DasClient client = DasClientFactory.getClient("MySqlSimple"); SqlBuilder sqlBuilder = SqlBuilder.selectCount().from(Person.PERSON).intoObject(); Number before = client.queryObject(sqlBuilder); DataSourceConfigure oldConfig = DataSourceConfigureLocatorManager.getInstance().getDataSourceConfigure("dal_shard_0"); DataSourceConfigure newConfig = new DataSourceConfigure(oldConfig.getName(), oldConfig.getProperties()); DataSourceConfigureChangeEvent event = new DataSourceConfigureChangeEvent("testEvent", newConfig, oldConfig); ConcurrentHashMap<String, DataSource> dss = (ConcurrentHashMap<String, DataSource>) FieldUtils.readStaticField(DataSourceLocator.class, "cache", true); RefreshableDataSource dataSource = (RefreshableDataSource) dss.get("dal_shard_0"); SingleDataSource oldSingleDataSource = ((AtomicReference<SingleDataSource>)FieldUtils.readField(dataSource, "dataSourceReference", true)).get(); dataSource.configChanged(event); Number after = client.queryObject(sqlBuilder); assertNotSame(oldSingleDataSource, ((AtomicReference<SingleDataSource>)FieldUtils.readField(dataSource, "dataSourceReference", true)).get()); assertEquals(before, after); }