How to Learn db40, Java and .NET Object Database, in 5 minutes

By Angsuman Chakraborty, Gaea News Network
Sunday, March 15, 2009

db4o is a popular object database available both for Java and .NET. I have used it sporadically over several years and can highly recommend it. It is a non-intrusive, very simple to learn, object persistence system that stores any complex object with one single line of code. Unfortunately Db4o is still not as popular with Java developers as we thought it would be. So we thought of giving you a very easy and useful guide to learning db4O. The following guide will focus on Java

Overview

Db4o allows you to persist any regular Java objects, including but not limited to container objects like Hashtable, Vectpr, Hashmap etc. Internally it uses Java reflection to do the magic.

How to open db4o database

To access a db4o database file or create a new one, call Db4o.openFile() and provide the path to your  database file as the parameter, to obtain an ObjectContainer instance:

ObjectContainer db = Db4o.openFile("db");

Now use db to fetch and / or save objects. A database file can only be opened once, subsequent attempts to open another ObjectContainer against the same file will result in a DatabaseLockedException.

Database files can only be accessed for readwrite access from one process (one Java VM) at one time. All versions except for db4o mobile edition use an internal mechanism to lock the database file for other processes.

How to save objects in db4o database

To store an object, we simply call set() on our database, passing the object as a parameter.

Player player = new Player("Ronaldo");
db.set(player);

Not only it saves the object, it also saves the objects it contains, recursively to the depth you specify. This is very useful for saving container objects and their children.

How to retrieve an object from db4O object database

db4o supplies three different querying systems: Query by Example (QBE), Native Queries (NQ) and the SODA Query API (SODA).

QBE is the simplest and easiest to learn and in my experience suffices most requirements. Let’s fetch the player object using QBE:

Player p = new Player("Ronaldo");
ObjectSet ronaldo = db.get(p);

Now you can use and modify ronaldo object and then later you can also save it to the database.

Gotcha: set() does both insert and update. If you save an object which was fetched earlier from the database then the object is updated in database. If you create a new object and save it, even if it contains the exact same data as an existing object, it will still be inserted as a new object in the database. While confusing for newcomers, it makes perfect sense logically.

How to delete an object from db4o database

First you need to fetch the object from the database using get() and then delete the object using delete(). To delete the player object we created earlier:

Player p = new Player("Ronaldo");
ObjectSet ronaldo = db.get(p);
db.delete(ronaldo);

Note: delete() has to be called for every single object individually. Delete does not recurse to object members. Simple and array member types are destroyed.

How to close db4o database

Any good database requires that it is gracefully closed. Db4o database can be closed by calling ObjectContainer.close(). In our example db is the ObjectContainer. So we should call db.close() after we are done using db4o. A call to close() automatically performs a commit().

Note that every session opened with Db4o.openFile() requires one close() call, even if the same file name was used multiple times.

Use while(!close()){} to kill all sessions using this container.

Recent versions of db4o will automatically close the database for you on exit. However it is advisable that you manually close the database.

In conclusion…

Now you know enough of db4o to build any application with it. Db40 is available under GPL. For re-distributing your application, you will have to license it from them. Last I checked, the pricing was pretty reasonable.

Did you like my db40 tutorial? Please let me know in the comments below. Also digg it or submit to any social networking / bookmarking tool of your choice. That will encourage me to write more such tutorials :)

Discussion

stan
January 16, 2010: 6:49 pm

Try EffiProz (www.EffiProz.com). It’s a Full Featured database written in C#.

November 20, 2009: 7:22 am

Which database we can open with db4o

Can I open MS SQL Server 2005 mdf file with this ..
urgent

November 13, 2009: 11:23 pm

Well,I like the technique which says that it can possible for the person to learn all the things in just five minutes.It is right but the recollection in that case.I agree with that the point that Db40 is available under GPL. For re-distributing your application, you will have to license it from them. Last I checked, the pricing was pretty reasonable.GPL has given good facilities for the databases.I want to know suggestion from others.

April 10, 2009: 3:12 am

In any project where you want to use an object database or a RDBMS :)

April 9, 2009: 9:12 am

Nice article!!

can you post sample scenario where i can use this?

March 16, 2009: 4:12 am

I have updated the post with more information. Check it out.

March 16, 2009: 2:36 am

Nice! Thanks a lot for the quick intro!!
German

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :