Documente Academic
Documente Profesional
Documente Cultură
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.
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.
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:
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.
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.
Note that every session opened with Db4o.openFile() requires one close() call,
even if the same file name was used multiple times.
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