2014-04-01 68 views
2

我正在獲取Databse鎖定問題。我有一個擴展Activity的類,只創建數據庫的實例。 這是我的超類數據庫鎖定問題android

public class Example extends Activity 
{ 

    protected DBUtil mDbUtil; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    mDbUtil = DBUtil.getInstance(getApplicationContext()); 
    } 

    @Override 
    public void finish() { 
     super.finish(); 
     mDbUtil.close(); 
    } 
} 

這是我的數據庫類

public class DBUtil extends SQLiteOpenHelper { 
    public static DBUtil getInstance(Context context) { 
      if(mInstance == null && context != null) { 
       mInstance = new DBUtil(context); 
       mDatabase = mInstance.getWritableDatabase(); 

      } 
      if(mDatabase!=null && !mDatabase.isOpen()) 
      { 
       mDatabase=mInstance.getWritableDatabase(); 
      } 
      return mInstance; 
     } 


public boolean replaceOrUpdate(DBListener dbListener,final String sTable, ContentValues[] contentValues) { 
     this.mDbListener=dbListener; 

     if(mDatabase == null) { 
      mDatabase = getWritableDatabase(); 
     } 
     mDatabase.beginTransaction(); 
     try { 
      int count = contentValues.length; 
      for (int i=0; i<count;i++) { 
       ContentValues value = contentValues[i]; 
       long id = mDatabase.replaceOrThrow(sTable,null,value); 
       debug("Insert id = " + id); 
      } 
      mDatabase.setTransactionSuccessful(); 
      mDbListener.onCompleteInsertion(); 
     }catch (Exception e) { 
      Log.v("Exception = " , " " + e.toString()); 
      e.printStackTrace(); 
     }finally { 
      mDatabase.endTransaction(); 
     } 
     return true; 
    } 

    public boolean replaceOrUpdate(final String sTable, ContentValues[] contentValues) { 

     if(mDatabase == null) { 
      mDatabase = getWritableDatabase(); 
     } 
     mDatabase.beginTransaction(); 
     try { 
      int count = contentValues.length; 
      for (int i=0; i<count;i++) { 
       ContentValues value = contentValues[i]; 
       long id = mDatabase.replaceOrThrow(sTable,null,value); 
       debug("Insert id = " + id); 
      } 
      mDatabase.setTransactionSuccessful(); 
     }catch (Exception e) { 
      Log.v("Exception = " , " " + e.toString()); 
     }finally { 
      mDatabase.endTransaction(); 
     } 
     return true; 
    } 

     public void close() { 
      if(mDatabase != null) { 
       miDbOpenCounter--; 
       mDatabase.close(); 
      } 
      mInstance = null; 
      mDatabase = null; 
     } 

    } 

我的所有活動來延長例

我打開活動A(沒有完成) - >活動B(沒有完成它) - >活動C 活動c返回結果並通過在Db中插入後調用finish()來關閉自身。但是,當插入它在這裏給我mDatabase.endTransaction();錯誤的空指針異常,然後數據庫鎖定異常

04-01 18:19:24.624: I/SQLiteConnectionPool(25638): The connection pool for +data+data+com_example_Example+databases+Example_db has been closed but there are still 1 connections in use. They will be closed as they are released back to the pool. 


04-01 18:19:24.624: W/System.err(25638): java.lang.NullPointerException 
04-01 18:19:24.639: W/System.err(25638): at com.example.examp.DBUtil.replaceOrUpdate(DBUtil.java:150) 
04-01 18:19:24.639: W/System.err(25638): at com.example.examp.SignupMerchant.onCompleted(SignupMerchant.java:342) 
04-01 18:19:24.639: W/System.err(25638): at com.example.examp.ExampleAsyncTask.onPostExecute(ExampleAsyncTask.java:284) 
04-01 18:19:24.639: W/System.err(25638): at com.example.examp.ExampleAsyncTask.onPostExecute(ExampleAsyncTask.java:1) 
04-01 18:19:24.639: W/System.err(25638): at android.os.AsyncTask.finish(AsyncTask.java:631) 
04-01 18:19:24.639: W/System.err(25638): at android.os.AsyncTask.access$600(AsyncTask.java:177) 
04-01 18:19:24.639: W/System.err(25638): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644) 
04-01 18:19:24.639: W/System.err(25638): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-01 18:19:24.639: W/System.err(25638): at android.os.Looper.loop(Looper.java:176) 
04-01 18:19:24.684: D/dalvikvm(25638): GC_FOR_ALLOC freed 1556K, 27% free 22981K/31248K, paused 43ms, total 43ms 
04-01 18:19:24.684: W/System.err(25638): at android.app.ActivityThread.main(ActivityThread.java:5419) 
04-01 18:19:24.684: W/System.err(25638): at java.lang.reflect.Method.invokeNative(Native Method) 
04-01 18:19:24.684: W/System.err(25638): at java.lang.reflect.Method.invoke(Method.java:525) 
04-01 18:19:24.684: W/System.err(25638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
04-01 18:19:24.684: W/System.err(25638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
04-01 18:19:24.684: W/System.err(25638): at dalvik.system.NativeStart.main(Native Method) 


04-01 18:19:27.524: E/SQLiteLog(25638): (5) database is locked 
04-01 18:19:27.544: E/SQLiteDatabase(25638): Failed to open database '/data/data/com.example.examp/databases/Example.db'. 
04-01 18:19:27.544: E/SQLiteDatabase(25638): android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode 
04-01 18:19:27.544: E/SQLiteDatabase(25638): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
04-01 18:19:27.544: E/SQLiteDatabase(25638): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118) 
04-01 18:19:27.544: E/SQLiteDatabase(25638): at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:825) 

任何想法,以擺脫這種?

回答

0

您的close()方法會刪除所有活動之間共享的變量static,無論數據庫在某些其他活動中仍處於打開狀態。

考慮重新設計您的應用程序,以便您在此處沒有static變量。每個活動都可以創建自己的數據庫連接。

+0

如果我只有當我的應用程序退出時關閉數據庫連接才能解決問題嗎? – Manoj

+0

它可能會防止崩潰,但不能解決靜態數據及其共享所有權的固有問題。最佳解決方案通常是通過消除靜態來消除共享所有權。 – laalto

+0

很酷。我去做。我將刪除靜態實現並將切換回每個活動的一個實例。 – Manoj