开发者

Opening database managing handles

I am using berkeley database....

Generally at this type of database you open an environment which is a bunch of files to control locking and transactions etc and then you open your database in this environment...

The problem is that there are LOTS of databases to open....

The method to open a database is opendatabase()

However opening and closing the database always is slow... The documentation says

Opening a database is a relatively expensive operation, and maintaining a set of open databases will normally be preferable to repeatedly opening and closing the database 开发者_StackOverflow中文版for each new query.

The problem is how to maintain that set ??????

A simple solution i thought was lazy loading

 private static Database db;
     public   CustomerDAO() {
            if (db == null) {
                try {
                    DatabaseConfig dbConfig = new DatabaseConfig();
                    dbConfig.setAllowCreate(true);
                    dbConfig.setType(DatabaseType.BTREE);
                    db = BDBEnvironment.DEFAULT.getEnvironment().openDatabase(null, "C:\\xxxx\\CUSTOMERS",
                            null, dbConfig); 

But this has a problem with double check locking.. Right???

Another issue is that i want to have a default file name or a user specified one.. Of course it is easy to create a DatabaseManager but always double check lock issue would occur.

Any ideas how to maintain a set of database handles??


Use basic Java synchronization techniques and a thread-safe data structure such as a ConcurrentHashMap to store your DB handles. You should probably read this book if you haven't already, as it covers a lot of what you'll need for this kind of issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜