2011-12-20 59 views
3

我在Android 3.0+上遇到了db4o問題,因爲事實證明,在創建db4o數據庫時,默認情況下會使用某些網絡apis。 (我偶然發現了這個帖子:http://mavistechchannel.wordpress.com/2011/11/18/db4o-at-honeycomb-and-ice-cream-sandwich/關於它)Android上的db4o 3.0+問題

但是,我試圖讓數據庫創建請求異步,但我想我遇到了一個問題,因爲它在它完全創建之前調用數據庫DB。 (我現在得到一個鎖定錯誤)有什麼辦法可以做到這一點同步?或者,至少等到它完成了?這裏是我的db4o幫手:

public class Db4oHelperAsync implements Constants{ 

private static final String USE_INTERNAL_MEMORY_FOR_DATABASE = "USE_INTERNAL_MEMORY_FOR_DATABASE"; 

private static ObjectContainer oc = null; 
private Context context; 

/**  
* @param ctx 
*/ 

public Db4oHelperAsync(Context ctx) { 
    context = ctx; 
} 

/** 
* Create, open and close the database 
*/ 
public ObjectContainer db() { 


    if (oc == null || oc.ext().isClosed()) { 

     if (Utilities.getPreferences(context).getBoolean(USE_INTERNAL_MEMORY_FOR_DATABASE, true)) { 

      new GetDbFromInternalMemory().execute(); 

     } else { 
      new GetDbFromSDCard().execute(); 
     } 
     return oc; 


    } else { 

     return oc; 

    } 

} 
/** 
* Configure the behavior of the database 
*/ 

private EmbeddedConfiguration dbConfig() throws IOException { 
    EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); 

    configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).objectField("name").indexed(true); 
    configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnUpdate(true); 
    configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnActivate(true); 
    configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnDelete(true); 

    configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).objectField("name").indexed(true); 
    configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).cascadeOnUpdate(true); 
    configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).cascadeOnActivate(true); 

    return configuration; 
} 

/** 
* Returns the path for the database location 
*/ 

private String db4oDBFullPathInternal(Context ctx) { 
    return ctx.getDir("data", 0) + "/" + "testapp.db4o"; 
} 

private String db4oDBFullPathSdCard(Context ctx) { 
    File path = new File(Environment.getExternalStorageDirectory(), ".testapp"); 
    if (!path.exists()) { 
     path.mkdir(); 
    } 
    return path + "/" + "testapp.db4o"; 
} 

/** 
* Closes the database 
*/ 

public void close() { 
    if (oc != null) 
     oc.close(); 
} 

private class GetDbFromInternalMemory extends AsyncTask<Void, Void, ObjectContainer>{ 

    @Override 
    protected ObjectContainer doInBackground(Void... params) { 
     try { 
      ObjectContainer obj = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPathInternal(context)); 
      CLog.v("USING INTERNAL MEMORY FOR DATABASE"); 
      return obj; 

     } catch (Exception ie) { 
      ie.printStackTrace(); 
      CLog.e(Db4oHelper.class.getName(), ie.toString()); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(ObjectContainer result) 
    { 
     oc = result; 
    } 
} 

private class GetDbFromSDCard extends AsyncTask<Void, Void, ObjectContainer>{ 

    @Override 
    protected ObjectContainer doInBackground(Void... params) { 
     try { 

      ObjectContainer obj = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPathSdCard(context)); 
      CLog.v("USING SDCARD FOR DATABASE"); 
      SharedPreferences.Editor edit = Utilities.getPreferencesEditor(context); 
      edit.putBoolean(USE_INTERNAL_MEMORY_FOR_DATABASE, true); 
      edit.commit(); 

      return obj; 

     } catch (Exception ie) { 
      ie.printStackTrace(); 
      CLog.e(Db4oHelper.class.getName(), ie.toString()); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(ObjectContainer result) 
    { 
     oc = result; 
    } 
} 
} 

回答

2

更新:This db4o bug has been fixed。如果你得到最新的8.1位,錯誤不應該發生,解決方法是絕對的:

當你試圖獲取數據庫時,你會得到一個文件鎖定的異常?對。

那麼問題是,你沒有等待異步任務完成,只是在Db4oHelperAsync.oc爲空的情況下啓動一個新任務。您基本上必須等到初始化完成後才使用Db4oHelperAsync.oc變量。所以你在Java同步領域。

例如,您可以這樣做:同步Db4oHelperAsync.oc訪問權限。當請求數據庫等待,直到變量被設置。不幸的是,我不知道異步任務的確切行爲。我的猜測是,它會在主要活動中運行.onPostExecute()方法。這也意味着你不能等待它,因爲這意味着你阻止了Activity-Thread並且.onPostExecute()永遠不會被執行。

這是我的草稿,我會盡力去做。我從來沒有執行也沒有編譯它。它可能有同步問題。例如,當初始化失敗時,它只會將應用程序掛在.db()調用上,因爲它會一直等待。所以要非常小心,並試圖改善它:

public class Db4oHelperAsync implements Constants{ 

    private static final String USE_INTERNAL_MEMORY_FOR_DATABASE = "USE_INTERNAL_MEMORY_FOR_DATABASE"; 

    private static ObjectContainer oc = null; 
    private static final Object lock = new Object(); 
    private Context context; 

    /**  
    * @param ctx 
    */ 

    public Db4oHelperAsync(Context ctx) { 
     context = ctx; 
    } 

    /** 
    * Create, open and close the database 
    */ 
    public ObjectContainer db() {  
     synchronized(lock){ 
      if (oc == null || oc.ext().isClosed()) { 
       if (Utilities.getPreferences(context).getBoolean(USE_INTERNAL_MEMORY_FOR_DATABASE, true)) { 
        new GetDbFromInternalMemory().start(); 
       } else { 
        new GetDbFromSDCard().start(); 
       } 
       while(oc==null){ 
        this.wait() 
       } 
       return oc; 
      } else { 
       return oc; 
      } 
     } 
    } 
    /** 
    * Configure the behavior of the database 
    */ 

    private EmbeddedConfiguration dbConfig() throws IOException { 
     EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration(); 

     configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).objectField("name").indexed(true); 
     configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnUpdate(true); 
     configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnActivate(true); 
     configuration.common().objectClass(PersistentObjectWithCascadeOnDelete.class).cascadeOnDelete(true); 

     configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).objectField("name").indexed(true); 
     configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).cascadeOnUpdate(true); 
     configuration.common().objectClass(PersistentObjectWithoutCascadeOnDelete.class).cascadeOnActivate(true); 

     return configuration; 
    } 

    /** 
    * Returns the path for the database location 
    */ 

    private String db4oDBFullPathInternal(Context ctx) { 
     return ctx.getDir("data", 0) + "/" + "testapp.db4o"; 
    } 

    private String db4oDBFullPathSdCard(Context ctx) { 
     File path = new File(Environment.getExternalStorageDirectory(), ".testapp"); 
     if (!path.exists()) { 
      path.mkdir(); 
     } 
     return path + "/" + "testapp.db4o"; 
    } 

    /** 
    * Closes the database 
    */ 

    public void close() { 

     synchronized(lock){ 
      if (oc != null) 
       oc.close(); 
     } 
    } 

    private class GetDbFromInternalMemory extends Thread{ 

     @Override 
     protected void run() { 
      try { 
       ObjectContainer obj = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPathInternal(context)); 
       CLog.v("USING INTERNAL MEMORY FOR DATABASE"); 

       synchronized(Db4oHelperAsync.lock){ 
        Db4oHelperAsync.oc = obj; 
        Db4oHelperAsync.lock.notifyAll() 
       } 
      } catch (Exception ie) { 
       ie.printStackTrace(); 
       CLog.e(Db4oHelper.class.getName(), ie.toString()); 
      } 
     } 
    } 

    private class GetDbFromSDCard extends Thread{ 

     @Override 
     protected void run() { 
      try { 
       ObjectContainer obj = Db4oEmbedded.openFile(dbConfig(), db4oDBFullPathSdCard(context)); 
       CLog.v("USING SDCARD FOR DATABASE"); 
       SharedPreferences.Editor edit = Utilities.getPreferencesEditor(context); 
       edit.putBoolean(USE_INTERNAL_MEMORY_FOR_DATABASE, true); 
       edit.commit(); 

       synchronized(Db4oHelperAsync.lock){ 
        Db4oHelperAsync.oc = obj; 
        Db4oHelperAsync.lock.notifyAll() 
       } 
      } catch (Exception ie) { 
       ie.printStackTrace(); 
       CLog.e(Db4oHelper.class.getName(), ie.toString()); 
      } 
     } 
    } 
} 

P.S.作爲db4o的一個bug添加了這個問題:http://tracker.db4o.com/browse/COR-2269

+0

感謝您的幫助 - 我沒有完全遵循這一點,但足夠接近。這有點令人沮喪,但是,哦! – Hosemeyer 2011-12-22 18:56:57

1

感謝您發佈這個問題,這是一個嚴重的Android樂趣擾流板。 創建新的db4o數據庫文件時,db4o通過調用java.net.InetAddress.getLocalHost()。getHostName()來生成它的唯一內部簽名。這個電話沒有發現異常情況。我們將爲Android找到一個解決方法,並在這裏修復時回到這裏和我們的論壇。

2012年2月9日更新: 該問題已得到修復,新版本在線。 http://community.versant.com/Blogs/db4o/tabid/197/entryid/1057/Default.aspx