Using Db4o in an Android Application


Db4o is an object database, i.e., forget about the mapping of tables in a relational design. If you happen to be a developer, that interprets into price savings in time invested in your software and volume of code. Db4o’s good opportunity is that you can reuse your [plain, non-mapped] objects by preserving and retrieving them as a lot of periods as you want. You can persist complex objects with nested collections or other intricate objects at any stage of complexity in your hierarchy. You happen to be not limited to flat objects with primitive styles to get reasonable functionality.

With db4o you can also gain from indigenous queries which deliver you nearer to the language and merge optimized execution with simplicity even for advanced queries. Db4o is open supply and can be downloaded in this article.

In purchase to use db4o in your application, you can basically reference a solitary jar file in your challenge, and that is it: no setup, no server, and no intricate installation. For a Java venture applying Eclipse, you just fall the jar file in the “lib” folder and insert the library to the establish route. In accordance to your requires, you need to decide on the db4o-xxx-yyy.jar file to be integrated. If “xxx” is “all,” you get all the functionality of db4o. If it’s “main,” you get the really minimum amount. With regards to “yyy” is dependent on what edition of Java you want to help (e.g., db4o-xx-core-java5.jar targets Java JDK 5 and JDK 6).

How To Use db4o in Android

I really delight in performing with db4o on Android. I typically start off by building a central course (e.g., Db4oHelper) that handles the db4o API and controls when to create, open, and near the database.

public course Db4oHelper {
    non-public static ObjectContainer oc = null
    non-public Context context
    /**            * @param ctx     */
    community Db4oHelper(Context ctx)
        context = ctx

    /**    * Build, open and shut the databases    */
    general public ObjectContainer db()
            if (oc == null  capture (Exception ie)
            Log.e(Db4oHelper.class.getName(), ie.toString())
            return null

     /**    * Configure the actions of the database    */
    private EmbeddedConfiguration dbConfig() throws IOException
        EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration()
        configuration.typical().objectClass(Work out.class).objectField("identify").indexed(genuine)
        configuration.typical().objectClass(Work out.course).cascadeOnUpdate(correct)
        configuration.common().objectClass(Physical exercise.course).cascadeOnActivate(genuine)
        return configuration
     /**       * Returns the path for the databases spot       */
    private String db4oDBFullPath(Context ctx)
        return ctx.getDir("facts", ) + "" + "pumpup.db4o"
     /**       * Closes the databases       */
    public void near()
        if (oc != null) oc.near()


Next, we can develop a Service provider for every single of our lessons where we can implement the connected queries (e.g., to tackle persistence of Exercises). We can make it extend Db4o Helper to get all the routine maintenance strategies (open up, shut, etcetera.).

public course ExerciseProvider extends Db4oHelper {
general public remaining static String TAG = "ExerciseProvider"
non-public static ExerciseProvider provider = null              community
ExerciseProvider(Context ctx)
tremendous(ctx)                            general public static
ExerciseProvider getInstance(Context ctx)
if (service provider == null)
service provider = new ExerciseProvider(ctx)
return supplier                            community void
retailer(Workout training) {
delete(Work out physical exercise) {
findAll() {                            return

It really is genuinely critical not to overlook to near the databases in advance of exiting the software. Usually, your modifications is not going to be dedicated. A great way to implement this in Android is in the Launcher action (major activity for the application) where we’ll develop an occasion of db4o Helper and invoke close() upon exit.
public class key extends Activity
non-public Db4oHelper db4oHelper = null              @Override
public void onCreate(Bundle icicle)
setContentView(R.format.major)                            dbHelper()
            /**              * Build Db4oHelper instance
*/              personal Db4oHelper dbHelper()
if (db4oHelper == null)
db4oHelper = new Db4oHelper(this)
return db4oHelper                                       /**
* Shut databases before exiting the software              */
@Override              safeguarded void onPause()
super.onDestroy()                            dbHelper().close()
db4oHelper = null              

Working with db4o on Android is a nice choice if you’re not pressured to perform with Content Suppliers, which are only essential if you ought to share information with other exterior purposes and are much too coupled to Android’s SQLite. For dealing with all persistence aspects of a one software, db4o is a pleasure to work with: it creates no head aches, it will save a large amount of time, and is basic and easy.


Please follow and like us:
Content Protection by