Android SQLite 数据库在事务期间崩溃后锁定
如果我的应用程序在事务期间被终止(即在下面源代码中的位置 1),则数据库将被锁定。该应用程序是使用任务管理器或从调试器内终止来终止的(我知道不应使用任务管理器,并且我知道在此阶段终止应用程序是我的错;-)) - 但无论如何,这种情况可能会发生。
如果我随后尝试重新启动应用程序,sqlite 无法打开数据库,因为它已被锁定(即使在设备重新启动后)。文件系统上有数据库文件和日志文件。如何恢复数据库并再次打开它?
final SQLiteDatabase db = mDb.getDb(); try { db.beginTransaction(); //execute some updates (1) db.setTransactionSuccessful(); } finally { db.endTransaction(); }
此时数据库只有一个线程访问,数据库只有一个实例。
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 only one instance of the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用的是在打开数据库之前删除日志文件......
或者以某种方式处理损坏的日志文件。
What worked for me is to delete the journal file before opening the database...
Or handle the corrupt journal file somehow.