Channels ▼


Inside the db4o Database

Source Code Accompanies This Article. Download It Now.

An Implementation

db4o from db4objects ( is an open-source object database that provides the capabilities that I've described. db4o is available in Java and .NET flavors; in this article, I use its Java version.

Besides being open source, db4o's features include an easy-to-grasp API, equally lucid persistence model, and the fact that it is a true object database (not an object/relational mapping database). Add to this its invisible transactioning and its adherence to ACID (atomicity, consistency, isolation, and durability) behavior, and you have an embeddable database that concentrates a lot of power in a small area.

The entire db4o library is provided in a single .JAR file (or .DLL if you're using .NET), and you'll typically find yourself handling better than 90 percent of your database work with about 10 API calls. In addition, as far as db4o is concerned, all objects are persistence capable. Classes whose objects are to be persistent need not be derived from a persistence-aware base class, nor must persistent classes implement any specialized persistence interface. If you want to put an object into the database, you more or less tell db4o, "Please put this object into the database," and it does the rest.

In fact, db4o doesn't need any description of a persistent object's structure (a schema file). When you tell db4o to put an object in the database, the runtime engine uses reflection to "spider" the object's architecture and deduce how the object is to be stored. The spidering works in reverse, too; when you fetch an object, db4o need not be told anything about that object to properly instantiate it.

Replication is relatively new in db4o, having appeared in the 5.0 release. Replication support within a given database is optional, and for good reason. Again, replication requires that db4o attach a universal identifier to each object, as well as maintain versioning information. This necessarily consumes space in the database, so it should be enabled only when required.

Specifically, the universal identifier that db4o constructs is composed of two parts:

  • An object ID number. Every object in the database has associated with it a unique 32-bit long. This identifier is unique within the database.
  • A database signature. This signature is associated with the entire database. Typically, it is about 30 bytes long, and is derived from a variety of sources. For example, one element used is the host name of the computer that the database was originally created on. The signature is guaranteed unique for all db4o databases.

So, the logical concatenation of the signature with the object ID provides a truly universal identifier for any object in any db4o database.

This, however, is not all the information you need for complete replication support. Again, versioning must be attached to objects so that modifications can be properly reconciled at synchronization. To this end, when databases are replicated, db4o records the transaction number (in both databases) of the replication operation. This transaction number effectively becomes the object's version number. (The original transaction number is also retained.) Any subsequent modification to the object causes its version number to be incremented. Consequently, when synchronization takes place at a future point, db4o compares version numbers to see which objects have been modified since the last replication.

Here, db4o takes a cautious approach. It does not presume that the object with the higher version number is the winner. Instead, db4o uses the following replication plan:

  • If neither object has changed since the last replication, nothing happens.
  • If one object has changed, and the other has not, the changed object wins (overwriting the loser).
  • If both objects have changed, then a user-supplied conflict resolution routine is called.

Consequently, db4o passes off to you the problem of determining which object is the more current. db4o does not assume that a bigger version number indicates a later object. The aforementioned conflict resolution routine is a sort of deus ex machina method that must be supplied to the replication process, and that steps in to decide which object is the winner.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.