Java Programming with Berkeley DB XML

来源:互联网 发布:鲁豫怎么这么瘦 知乎 编辑:程序博客网 时间:2024/04/29 07:21

Java Programming with Berkeley DB XML

Java Programming with Berkeley DB XML—Berkeley DB XML (BDB XML) is a popular native XML database. It can beaccessed through the shell or within another program. This month I willshow you how to use BDB XML in Java. BDB XML has similar APIs for allsupported languages such as Java and C++, therefore the ideas presentedin this article apply to all supported languages. I have been closelyfollowing BDB XML from the very first release, and there have beentremendous improvements in this product.

Berkeley DB XML (BDB XML) is a popular native XML database. It canbe accessed through the shell or within another program. This month Iwill show you how to use BDB XML in Java. BDB XML has similar APIs forall supported languages such as Java and C++, therefore the ideaspresented in this article apply to all supported languages. I have beenclosely following BDB XML from the very first release, and there havebeen tremendous improvements in this product.

Last month in this column I wrote about using BDB XML through the command shell (WSJVol. 5, iss. 12). I also mentioned some of the basic BDB XML concepts,which I think may be helpful in constructing Java programs. If youaren't familiar with BDB XML concepts, I recommend reading last month'sarticle or checking the online BDB XML documentation.

Installation
BDB XML is distributed by SleepyCatSoftware, and has an installer for the Windows operating systems. Youmay choose to build it from the source files, which exist for almostevery popular operating system. For convenient coding I used theEclipse Integrated Development Environment (IDE) from www.eclipse.org.Eclipse is a very nice IDE that makes programming tasks easy and fun.It's available free of charge. In order to use the Java API providedwith the BDB XML, we have to include two .jar files in the Classpath. These files are db.jar and dbxml.jar, and they should be located under the .jar directory of your installation path.

Environment
In order to start making something useful with our Java programs, wehave to take a moment to decide the properties of the environment thatwe will use throughout the lifetime of the code. I will explain some ofthe widely used environment properties. It's true that an embeddeddatabase such as BDB XML puts more pressure on the shoulders of theprogrammers. You should only enable a feature if there is a need forit. If you don't feel very comfortable with these database environmentfeatures, refer to a database book, or check them online.

An important feature of the Environment is that once it has beencreated, it's not possible to change many of its attributes. Thefollowing is a summary of commands:

  • EnvironmentConfig.setAllowCreate(true) - This command enables creation of the environment.
  • EnvironmentConfig.setInitializeLocking(true)- If there are multiple processes or multiple threads, this should beturned on. It locks the database sources for preventing anomalies. Datalocking is an important database concept. It's definitely needed in amultiple transaction environment. However, if the transactions areserial, i.e., one starts after the other finishes, you might not needit. In a multiple process or in a multiple thread environment, thereare competing transactions. In some situations deadlock may occurbecause of locking, but this feature comes with a deadlock detector forresolving such resource conflicts.
  • EnvironmentConfig.setInitializeLogging(true)- This command initializes the logging. Logging, which is essential forrecovery, is basically keeping track of read and write operations inthe database. When there is a power outage you may need to recoverusing the log files, and in some cases log files are used forreplication purposes.
  • EnvironmentConfig.setInitializeCache(true) - Initializes the shared memory. It must be set to true when using multithreaded applications.
  • EnvironmentConfig.setTransactional(true)- Enables transactional support that is essential for most of theapplications. A transaction is a set of read and write operations.Conceptually, a transaction looks something like this:
    Start the transaction:
    -Read customer credit card number
    -Contact Visa, verify the number
    -Charge the credit card account
    -Ship the item
    -Commit transaction

    The transaction above is an atomic operation; either all of theoperations succeed or none of them does. Imagine that there is a poweroutage after the third step ("Charge the credit card account"); thecredit card will be charged, but the item will not be shipped. This isnot good news for the customer. In a transactional database such asBerkeley DB XML, if there is a power outage after the third step, whenthe system is back on power, it will roll back all of the operationsdone in the first, second, and third steps. The credit card charge willbe refunded because the fourth step ("Ship the item") was notcompleted, so a customer won't be charged for something he didn'treceive.

  • EnvironmentConfig.setRunRecovery(true)- This will turn on the normal recovery feature, which makes sure thatthe database files are consistent with the log files. For example,after a power failure it's possible that the database files are behindthe logs. When the recovery is on, BDB XML will recover the databasefiles from the logs. This feature depends on the setInitializeLogging,which in fact must be turned on in advance. Listing 1 shows a Java snippet for configuring the environment.

原创粉丝点击