Channels ▼


Inside the db4o Database

Source Code Accompanies This Article. Download It Now.

Have "Patients"

To illustrate db4o's API, how replication is crafted, and how a conflict resolution method is supplied, I present a patient-weight monitoring database—one that a nutritionist might use to keep track of pounds that patients have added or lost.

The database holds objects from two classes: Patient and WeightEntry; see Listing One(a). (For clarity, I only show the data fields of the classes.)


public class Patient
{  private String name;
   private long patientID;
   private ArrayList weightHistory;
 ... methods for Patient ...
public class WeightEntry
{  private float weight;
   private long weightDate;
 ... methods for WeightEntry ...


public void addWeight(float _weight,
  long _timeMillis)
  // Create new WeightEntry
  WeightEntry _weightEntry =
    new WeightEntry(_weight, _timeMillis);
  // Attach it
Listing One

The fields defined for Patient are reasonably obvious; they hold the patient's name and a system-assigned patient identification number.

The weightHistory ArrayList tracks the patient's weight. So, when a new weight is recorded, a new WeightEntry object is created and placed on the end of the ArrayList. (We are assuming, here, that new weight recordings are placed at the end of the list, so it stays in a time-sorted order.)

Of course, before replicating anything, you have to get it into the database in the first place. First, let's define a method—a member of Patient—that lets you add an entry onto the ArrayList, as in Listing One(b) (also available electronically). This method instantiates a WeightEntry object, initializes its fields, and attaches it to the ArrayList. Now, you can create a patient and store his information into the database; see Listing Two.

// Make the database replication-capable

// Open a database (Create if it does not exist)
ObjectContainer patientDB = Db4o.openFile("PATIENTA.YAP");
// Create a new Patient
Patient _patient = new Patient(
  "John Doe",
// Add a weight
// Put the new patient in the database
// Commit the transaction and close the database
Listing Two

This is really all you need to store the patient John Doe, whose ID is 001, and whose weight at the time this snippet was run was 190 lbs.

Again, db4o's API is remarkably succinct. First, the class that models a database is db4o's ObjectContainer. To put an object into a database, you merely need to pass a reference to that object to the ObjectContainer's set() method. The db4o engine discovers the object's structure invisibly, regardless of the depth and complexity of that structure. So, in our example, not only is the Patient object stored, but its member ArrayList (weightHistory) and the WeightEntry object (within the ArrayList) are all stored as well. This characteristic—that all objects referenced by an object that is persistent are also persistent—is referred to as "persistence by reachability."

Notice that, before I opened the database, I called a pair of methods that configured the db4o engine to make the database replication capable. Specifically, I set global parameters so that any database subsequently created by the application would have UUIDs and version numbers enabled. Again, these options are going to cost us a bit in terms of performance and space consumed, so you only set them for databases to be replicated.

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.