Forum Stats

  • 3,770,452 Users
  • 2,253,116 Discussions
  • 7,875,461 Comments

Discussions

What is the best form to control a open of database?

530677
530677 Member Posts: 31
edited Sep 1, 2006 2:52PM in Berkeley DB Java Edition
Hi,

what is the best form to open a database?

I´m understood that Environment is unique for all aplication, and I think that it is good to put on Singleton Pattern, but I don´t know the best form to do it with the databases.

I think two forms, the first is create a singleton open database, with this form I don´t will close the database and all application will use it. So, the second form is open a data base at the moment that I will use it, for example, into a DAO when I will do inserts, deletes, etc, and at the final of the operation I close the database.

So, I don´t know what form is the best, and if those forms are corrects, if exist another form to do it I would like to know.

Comments

  • Hugo,

    An application can choose to use one or more Environment and Database instances in a single process. In other words, it is the application's choice whether it wants to instantiate a single Environment or Database and share it among multiple threads, or whether it wants to instantiate multiple instances.

    Choosing one pattern or another is a function of your application's design, and performance considerations. As you say, you could instantiate a single Database instance and pass it around, or you could open and close the Database every time you get a data record. A third choice is to open a Database instance per thread, and reuse that for the life of the application.

    The second choice of opening and closing the Database per data record access is pretty heavyweight from a performance point of view. There is a fair bit of overhead to the initial open of a database, and to the final close. Follow-on opens are not as heavyweight, but do have some cost. For example:

    // expensive
    thread 1 calls Environment.openDatabase() or new EntityStore()

    // less expensive, database is already open, thread 2 is really just getting a handle
    // onto the database, but still more expensive than a read or write of a data record
    thread 2 calls Environment.openDatabase() or new EntityStore()

    The first choice of using a single Database instance in your process will perform much better. If your application has a high level of concurrency, there can be some contention on the Database instance, described in this FAQ - http://www.oracle.com/technology/products/berkeley-db/faq/je_faq.html#32 and you may prefer to try the third option of using a Database instance per thread.

    Regards,

    Linda
  • 530677
    530677 Member Posts: 31
    Another one question...

    There are significant performance difference between only read open database and read/write open database?

    Hugo
  • Greybird-Oracle
    Greybird-Oracle Member Posts: 2,690
    Hi Hugo,

    There is no performance difference between opening a database read-write or read-only.

    Mark
This discussion has been closed.