数据库池

发布于 2024-11-07 08:32:32 字数 772 浏览 0 评论 0原文

你好,我正在尝试为 BerkeleyDB 实现一个数据库对象(“连接”)池化器... 我决定为此使用单例 EJB 或 ENUM 单例实现。

最终的并发哈希映射将存储带有时间戳的数据库对象...

方法 getConnection() 将使用双重检查锁定,只要地图中的值是不稳定的。 - 我相信没有性能问题..(Java 连接池 getConnection 已同步!!)
数据库分为 100 个文件 + 每日文件..(应用程序于 1976 年七十年代中期设计)..

到目前为止一切都很好...但我想关闭每日未使用的句柄。
所以我决定使用计时器每 24 小时运行一次清理例程..
问题是我如何确保在清理期间不请求关闭连接?
伪算法

    cleanup(){
      for(Database db in map){
         if(db.getLastAccess - now >24hours) {
               res=map.remove("key",db);
               db.close();
         }
      }
    }

我知道上面的内容不是线程安全的。我如何阻止 getconnection ?因为很多事情都可能出错...“如果条件”可能为真但是在删除 db obj getLastAccess 之前可以更改!不过,清理将由单线程调用。 是否有任何解决方案可以以某种方式阻止 getconnection 以便清理工作或任何其他解决方案?

Hello i am trying to implement a database-object("connection") pooler for BerkeleyDB...
I decided to use a singleton EJB propably or ENUM singleton implementation for this..

A final concurrenthash map would store database objects with a timestamp...

the method getConnection() would use double check locking as long as the value from map is volatile. - No performance issues i believe..(Java Connection Pooler getConnection is synchronized!!)
The database is spread into 100 files + the daily ones.. (application designed in mid seventies 1976)..

So far everything is fine... But i want to close daily unused handles.
So i decided to use a Timer to run every 24 hours a cleanup routine..
The problem is that how can i ensure that during cleanup a connection to be closed isnt requested ?
Pseudo algorithm

    cleanup(){
      for(Database db in map){
         if(db.getLastAccess - now >24hours) {
               res=map.remove("key",db);
               db.close();
         }
      }
    }

i know that the above isnt thread safe..How could i block getconnection ? Because many things could go wrong... "If condition" may be true but before removing db obj getLastAccess could be changed! Cleanup would be called by single thread though..
Is there any solution to block getconnection somehow so cleanup to work or anyother solution?

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

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

发布评论

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

评论(2

清风不识月 2024-11-14 08:32:32

我不确定您当前是否这样做,但如果您有办法确定连接是否正在使用,这将使这变得稍微容易一些。您可以做的一件事是迭代池中的连接。当您找到符合关闭标准的连接时,请尝试将其标记为正在使用(假设正在使用的连接不会作为打开的连接返回)。如果成功,请关闭它。否则,请检查它,直到它变为可用并且您可以将其标记为正在使用。一旦您能够执行此操作,您就应该能够关闭它。

I am not sure if you currently do this, but if you have a way to determine if a connection is in use this would make this slightly easier. One thing that you can do, is iterate over the connections in your pool. When you find one that matches your criteria for being closed, try to mark it as being in use (assuming that a connection that is in use will not be returned as a open connection). If you succeed, close it. Otherwise, check it until it becomes free and you are able to mark it as being in use. Once you have been able to do this, you should be able to close it.

甜妞爱困 2024-11-14 08:32:32

每个连接都有一个与其关联的锁,为了让 getConnection 方法返回连接,必须获取正确的锁。清理方法还需要在关闭连接之前获取锁。看一下 java.lang. util.concurrent.lock 包

也许 Semaphore 是更好的解决方案。请点击链接查看示例。

我从未使用过 BerkeleyDB,但我认为它有一个 JDBC 接口。您不能使用开箱即用的解决方案,例如 DBCPc3p0?另请检查池组件,它是一个通用池接口。

Each connection would have a lock associated with it, in order for the connection to be returned by the getConnection method, the correct lock would have to be acquired. The cleanup method would also need to acquire the lock before closing a connection. Take a look at the java.util.concurrent.lock package.

Maybe a Semaphore is a better solution. Follow the link for an example.

I've never worked with BerkeleyDB, but I assume it has a JDBC interface. Can't you use an out of the box solution like DBCP or c3p0? Also check the Pool Component, it is a generic pool interface.

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