Committing Event Example

Let's look at an example of committing event usage. In this example committing callback will be used to ensure that only unique objects can be saved to the database. Duplicate objects - objects, which have the same fields - will cause an exception at the commit time.

We will use a simple Item class for an example:

Item.java
01/* Copyright (C) 2007 db4objects Inc. http://www.db4o.com */ 02 03package com.db4odoc.commitcallbacks; 04 05 06public class Item { 07 private int _number; 08 private String _word; 09 10 public Item(int number, String word){ 11 _number = number; 12 _word = word; 13 } 14 15 public String getWord(){ 16 return _word; 17 } 18 19 public int getNumber(){ 20 return _number; 21 } 22 23 public String toString(){ 24 return _number + "/" + _word; 25 } 26}

The following methods will configure a commit-time callback method for uniqueness check:

CommitCallbackExample.java: configure
01private static void configure(){ 02 EventRegistry registry = EventRegistryFactory.forObjectContainer(container()); 03 // register an event handler, which will check object uniqueness on commit 04 registry.committing().addListener(new EventListener4() { 05 public void onEvent(Event4 e, EventArgs args) { 06 CommitEventArgs commitArgs = ((CommitEventArgs) args); 07 // uniqueness should be checked for both added and updated objects 08 checkUniqueness(commitArgs.added()); 09 checkUniqueness(commitArgs.updated()); 10 } 11 }); 12 }
CommitCallbackExample.java: checkUniqueness
01private static void checkUniqueness(ObjectInfoCollection collection){ 02 Iterator4 iterator = collection.iterator(); 03 while (iterator.moveNext()){ 04 ObjectInfo info = (ObjectInfo)iterator.current(); 05 // only check for Item objects 06 if (info.getObject() instanceof Item){ 07 Item item = (Item)info.getObject(); 08 // search for objects with the same fields in the database 09 ObjectSet found = container().get(new Item(item.getNumber(), item.getWord())); 10 if (found.size() > 1){ 11 throw new Db4oException("Object is not unique: " + item); 12 } 13 } 14 } 15 }

let's save one initial object Item(1,"one")

CommitCallbackExample.java: storeFirstObject
01private static void storeFirstObject(){ 02 ObjectContainer container = container(); 03 try { 04 // creating and storing item1 to the database 05 Item item = new Item(1, "one"); 06 container.set(item); 07 // no problems here 08 container.commit(); 09 } catch (Db4oException ex){ 10 System.out.println(ex.getMessage()); 11 container.rollback(); 12 } 13 }

Now we can check the functionality of the committing callback using the following code:

CommitCallbackExample.java: storeOtherObjects
01private static void storeOtherObjects(){ 02 ObjectContainer container = container(); 03 // creating and storing similar items to the database 04 Item item = new Item(2, "one"); 05 container.set(item); 06 item = new Item(1, "two"); 07 container.set(item); 08 try { 09 // commit should work as there were no duplicate objects 10 container.commit(); 11 } catch (Db4oException ex){ 12 System.out.println(ex.getMessage()); 13 container.rollback(); 14 } 15 System.out.println("Commit successful"); 16 17 // trying to save a duplicate object to the database 18 item = new Item(1, "one"); 19 container.set(item); 20 try { 21 // Commit should fail as duplicates are not allowed 22 container.commit(); 23 } catch (Db4oException ex){ 24 System.out.println(ex.getMessage()); 25 container.rollback(); 26 } 27 }