2

Has anyone developed a dynamic reload mechanism for the apache commons database configuration object?

Swapnil
  • 8,201
  • 4
  • 38
  • 57
YaOg
  • 1,748
  • 5
  • 24
  • 43

2 Answers2

3

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

Emmanuel Bourg
  • 9,601
  • 3
  • 48
  • 76
  • I love how this ticket was filed back in '05. Still waiting...8 years later. :) – Mark G. Mar 05 '13 at 21:30
  • :) I actually just wrapped this class with a cache provider (ehcache) for the time being. But this would be awesome to have provided by the lib! – Mark G. Mar 21 '13 at 20:32
0

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;

    }
}
rjdkolb
  • 10,377
  • 11
  • 69
  • 89