apache DatabaseConfiguration 中的动态重新加载

发布于 2024-09-13 11:07:18 字数 44 浏览 2 评论 0原文

有人为 apache commons 数据库配置对象开发了动态重载机制吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2024-09-20 11:07:19

实际上这是不必要的,因为 DatabaseConfiguration 不会缓存数据库中的值。每次获取属性时都会执行一次请求。有一个 RFE 可以缓存这些值以提高性能,这确实需要重新加载机制。

https://issues.apache.org/jira/browse/CONFIGURATION-180

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

且行且努力 2024-09-20 11:07:19

apache commons 数据库配置不支持缓存。

我扩展了 DatabaseConfiguration 以支持缓存,这样它就不会一直访问我的数据库。
至于重新加载,我在需要的地方实例化我的配置,并在完成后将其丢弃。

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;

    }
}

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;

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文