Biggest Db4o Gotcha!
By Angsuman Chakraborty, Gaea News NetworkMonday, August 18, 2008
Db4o is an excellent open source object database for Java & .NET platform by Carl Rosenberger’s team. I highly recommend it for rapid prototyping and RAD. It transparently handles object storage and retrieval. Today I will talk about the single biggest gotcha in Db4o which is bound to stumble any newcomer as well as pros who haven’t been using it in a while.
In Db4o you can create an complex objects with other objects as its member and Db4o will save them all like a champ with a single set() (now store()) method. So intuitively you try to use the same concept for updating the database. This is where you will stumble. Db4o by default doesn’t recurse when updating an object.The stranger aspect is that when you save it to the database with a store or set and then retrieve it again it will appear to work fine. However after you close and re-open the database your data will be lost! This is the single most baffling feature I found in Db4o.
The default recurse depth on update is just 1. However you can set the recursion depth on updating with this line of code:
Configuration config = Db4o.newConfiguration();
config.updateDepth(2);
ObjectContainer db = Db4o.openFile(config, “db”);
In earlier version it is:
Db4o.configuration().updateDepth(2);
ObjectContainer db = Db4o.openFile(”db”);
Change the update depth to as much as you need and only as much as you need.
Note: In the old versions you could change the configuration only globally. In the new 7.2 and above versions you can change it for each database.
BTW: To Db4o - I don’t particularly like the new method names - getQueryByExample() and store() or even newConfiguration(). I am very much used to get(), set() and configuration(). They are easy to use and easy to remember. Please cosnider not making them deprecated at the least.
Tags: Db4o, Lost, Object Database, Open Source, Open Source Software
Db4o Note: Db4o.configure() != Db4o.newConfiguration()