Home > General > db4o at Honeycomb and Ice Cream Sandwich – UPDATE

db4o at Honeycomb and Ice Cream Sandwich – UPDATE

Did you receive a NetworkOnMainThreadException while opening a new db4o database?

Since my last post I just worked with Andorid 2.x on which I could get db4o easily to run. When I wanted to run my db4o app at Android Honeycomb I received the NetworkOnMainThreadException exception.

Google introduced with Honeycomb the Android.os.NetworkOnMainThreadException which occurs when an application attempts to perform a networking operation on it’s main thread (see here). This doesn’t sound like it matters for a database environment. But that’s exactly what happens when I call


Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);

for the first time. Just for the first time because it seems that when db4o creates a new db file it generates it’s unique internal signature by calling java.net.InetAddress.getLocalHost().getHostName(). Because java.net belongs to the network classes the exception java.net.InetAddress.lookupHostByName(InetAddress.java:477) is thrown.

Here is the piece of db4o code which causes the exception:

try {
  String hostName = java.net.InetAddress.getLocalHost().getHostName() + "_";
    if(hostName.length() > 15){
      hostName = hostName.substring(0,15);
    }
  sb.append(hostName);
} catch (UnknownHostException e) {
}
sb.append(Long.toHexString(System.currentTimeMillis()));
sb.append(Integer.toHexString(_counter++));
int hostAddress = 0;
byte[] addressBytes;
try {
  addressBytes = java.net.InetAddress.getLocalHost().getAddress();
  for (int i = 0; i < addressBytes.length; i++) {
    hostAddress <<= 4;
    hostAddress -= addressBytes[i];
  }
} catch (UnknownHostException e) {
}

A workaround is to just catch the NetworkOnMainThreadException (okay, then it’s a android specific jar) or change the UnknownHostException to a generic Exception. This is kind of dirty but if nothing happens within the the catch it doesn’t matter anyways.

Another aproach would be to to preform the first call for opening a new db not into the UI Thread. A possibility could be to use the AsyncTask:


class OpendDBTask extends AsyncTask<Void, Void, ObjectContainer>{

   @Override
   protected ObjectContainer doInBackground(Void... params) {
      String dbPath = "/data/data/" + DBManager.getPackageName() + "/database";
      ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
      return db;
   }
}

Maybe I’ll provide a android database manager framework for db4o later. If you want to try db4o check out this post.

A bug entry has been created and it seems that it is fixed now. I didn’t retest yet.

Once again I would like to point out that this issue only occurs when a new db is created via “Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);” at Android Honeycomb or later!

Cheers, mavi

Categories: General Tags: , , ,
  1. 21/12/2011 at 13:35

    Oh, this is not cool and definitely an undesired side effect. The Db4oEmbedded.openFile really shouldn’t touch the network. I created a bug-entry.

    • 09/01/2012 at 01:51

      Thanks for reporting the bug. Should have done it myself already =)

  2. Carl Rosenberger
    05/01/2012 at 18:19

    Hi mavi!. Thanks for posting this issue.
    When a new db4o database file is created, db4o generates it’s unique internal signature by calling java.net.InetAddress.getLocalHost().getHostName(). Exceptions are not caught in this call. We will find a workaround for Android and post to our db4o forums when this is fixed.

    • 09/01/2012 at 02:08

      Hi Carl.
      Sounds convincing. But is it such a big deal to “compile” a db4o version just for android? Just catch the NetworkOnMainThreadException (okay, then it’s a android specific jar) or change the UnknownHostException to a generic Exception. This is kind of dirty but if you don’t do anything within the catch it doesn’t matter anyways. Both ways working fine for me but I don’t want to provide a jar here.
      In general it would be better to proivde a specific android jar download anyways. Even if it’s just for marketing reasons. It’s such a great project but not well know for android yet.
      Could you by any chance post the link if a workaround or a fix is released?

  3. Carl Rosenberger
    09/02/2012 at 17:16

    Hi mavi,
    the issue has been fixed and new builds are online on our website.
    Enjoy!

    http://community.versant.com/Blogs/db4o/tabid/197/entryid/1057/Default.aspx

    …and thanks again for making the issue public

  4. John Billinger
    03/03/2014 at 03:57

    It still doesn’t work for me. I am running db4o 8.0 and 8.1, but still neither work. I have the same exact problem as you, and it doesn’t seem like it got fixed by the db4o people…

  1. No trackbacks yet.

Leave a reply to mavi Cancel reply