You are here: Usage Pitfalls > Update Depth Pitfall

Update Depth Pitfall

db4o update behavior is regulated by Update Depth. Understanding update depth will help you to improve performance and avoid unnecessary memory usage. A common pitfall is that the update-depth is to small and that the objects are not updated. In such cases you either need to explicitly store the related objects individually or increase the update-depth.

For example in this code we add a new friend and store the object. Since a collection is also handled like a regular object, it is affected by the update-depth. In this example we only store the person-object, but not the friend-collection-object. Therefore with the default-update depth of one the update isn't store. In order to update this properly, you either need to set the update depth to two or store the friend-list explicitly.

using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
    Person jodie = QueryForJodie(container);
    jodie.Add(new Person("Jamie"));
    // Remember that a collection is also a regular object
    // so with the default-update depth of one, only the changes
    // on the person-object are stored, but not the changes on
    // the friend-list.
    container.Store(jodie);
}
using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
    Person jodie = QueryForJodie(container);
    foreach (Person person in jodie.Friends)
    {
        // the added friend is gone, because the update-depth is to low
        Console.WriteLine("Friend=" + person.Name);
    }
}
UpdateDepthPitfall.cs: Update doesn't work on collection
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
    Dim jodie As Person = QueryForJodie(container)
    jodie.Add(New Person("Jamie"))
    ' Remember that a collection is also a regular object
    ' so with the default-update depth of one, only the changes
    ' on the person-object are stored, but not the changes on
    ' the friend-list.
    container.Store(jodie)
End Using
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
    Dim jodie As Person = QueryForJodie(container)
    For Each person As Person In jodie.Friends
        ' the added friend is gone, because the update-depth is to low
        Console.WriteLine("Friend=" & person.Name)
    Next
End Using
UpdateDepthPitfall.vb: Update doesn't work on collection

So for this example setting the update-depth to two will fix the issue. For lots of operation a update-depth of two is pretty reasonable. This allows you to update collections without storing them explicitly.

IEmbeddedConfiguration config = Db4oEmbedded.NewConfiguration();
config.Common.UpdateDepth = 2;
using (IObjectContainer container = Db4oEmbedded.OpenFile(DatabaseFile))
{
UpdateDepthPitfall.cs: A higher update depth fixes the issue
Dim config As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
config.Common.UpdateDepth = 2
Using container As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFile)
UpdateDepthPitfall.vb: A higher update depth fixes the issue

When the update depth is set to a big value on objects with a deep reference hierarchy it will cause each update on the top-level object to trigger updates on the lower-level objects, which can impose a huge performance penalty.

Try transparent persistence, which removes the issue of the update-depth completly.