我在 Android 中使用 sqlite 事务:
SQLiteDatabase database = sqlite_helper.getWritableDatabase();
database.beginTransaction();
...
database.setTransactionSuccessful();
database.endTransaction();
我的问题是:
1. 我应该endTransaction()
像这样放在 finally 代码块中:</p>
try {
database.beginTransaction();
...
database.setTransactionSuccessful();
}
finally {
database.endTransaction();
}
如果在数据库操作过程中出现异常,数据库是否会自动回滚而不使用“finally”?
- 当事务没有结束时,其他线程可以读写同一个数据库吗?我听说 Android 中的 sqlite 是线程安全的,但我不确定。我想在交易过程中会有一些问题。如果另一个线程使用相同的连接写入相同的数据库,是否会引发错误?
我曾经在我的应用程序中发现这个错误,但我不知道它是否与线程安全问题有关:
android.database.sqlite.SQLiteMisuseException: library routine called out of sequence:
, 编译时
有人帮我回答这些问题吗?非常感谢!