dynamic reload in apache DatabaseConfiguration
Has anyone develo开发者_Go百科ped a dynamic reload mechanism for the apache commons database configuration object?
Actually this is not necessary because DatabaseConfiguration doesn't cache the values from the database. A request is performed every time a property is fetched. There is a RFE to cache the values for improved performance, and this will indeed require a reloading mechanism.
https://issues.apache.org/jira/browse/CONFIGURATION-180
apache commons database configuration does not support caching.
I extend DatabaseConfiguration to support caching so it does not hit my database all the time. As for reloads, I instantiate my config where I need it and throw it away when I am done with it.
MyConfig cfg = new MyConfig("jdbc/configdatabase");
public class MyConfig extends DatabaseConfiguration {
private WeakHashMap<String,Object> cache = new WeakHashMap<String,Object>();
public MyConfig(String datasourceString,String section) throws NamingException {
this((DataSource) new InitialContext().lookup(datasourceString),section);
}
protected MyConfig(DataSource datasource,String section) {
super(datasource, "COMMON_CONFIG","PROP_SECTION", "PROP_KEY", "PROP_VALUE",section);
}
@Override
public Object getProperty(String key){
Object cachedValue = cache.get(key);
if (cachedValue != null){
return cachedValue;
}
Object databaseValue = super.getProperty(key);
cache.put(key, databaseValue);
return databaseValue;
}
}
精彩评论