开发者

Android SQLite Database locked after crash during transaction

开发者 https://www.devze.com 2023-02-23 12:12 出处:网络
If my application gets killed during a transaction (i.e. at position 1 in the source code below) the database is locked. The app was killed either using a task manager or terminating from within the d

If my application gets killed during a transaction (i.e. at position 1 in the source code below) the database is locked. The app was killed either using a task manager or terminating from within the debugger (I know that taskmanager should not be used and I know that it is my fault terminating the application at this stage ;-)) - but anyway, this can happen.

If I then try to restart the application, sqlite can not open the database, because it is locked (even after a restart of the device). On the filesystem there is the database file and a journal file. How can I restore the database and open it again?

  final SQLiteDatabase db = mDb.getDb();
  try {
       db.beginTransaction();
       //execute some updates (1)
        db.setTransactionSuccessful();
   } finally {
        db.endTransaction();
   }

The database is accessed only by one thread at this time, and there is onl开发者_运维技巧y one instance of the database.


What worked for me is to delete the journal file before opening the database...

Or handle the corrupt journal file somehow.

0

精彩评论

暂无评论...
验证码 换一张
取 消