SQLiteDatabase 嵌套事务和解决方法 [英] SQLiteDatabase nested transaction and workaround

查看:57
本文介绍了SQLiteDatabase 嵌套事务和解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


是的,SQLite 不支持嵌套交易,但 docs 声明 SQLiteDatabase 确实如此.
情况
我有一个包含事务的方法,我需要从另一个事务调用这个方法.
此外 - 两个事务处理同一组记录,但更新不同的列.
问题
看起来我的外部交易的结果被内部交易取消了,仍然被 setTransactionSuccessful() 标记为干净并由 endTransaction() 完成 - 我已经检查过这个.
问题
- 知道为什么会发生这种情况吗?
- 是否有推荐的方法来进行此类交易?

Edited:
Yes, SQLite doesn't support nested transaction, but docs declare that SQLiteDatabase does.
Situation
I've got a method containing transaction and I need to call this method from another transaction.
Additionally - both transactions work on the same set of records, but update different columns.
Problem
It looks like results of my outer transactions are cancelled by inner one, still both are marked clean by setTransactionSuccessful() and finished by endTransaction() - I've checked this.
Questions
- Any idea why this may happen?
- Is there a recommended way to do such transactions?

推荐答案

您可以使用 Android sqlite API 嵌套事务,使用 注意事项:

You can nest transactions with the Android sqlite API, with caveats:

事务可以嵌套.当外部事务结束时,该事务中完成的所有工作以及所有嵌套事务都将被提交或回滚.如果任何事务在没有标记为干净的情况下结束(通过调用 setTransactionSuccessful),则更改将被回滚.否则他们将被提交.

Transactions can be nested. When the outer transaction is ended all of the work done in that transaction and all of the nested transactions will be committed or rolled back. The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

我见过的另一种通常与 sqlite 一起使用的方法是传入一个布尔参数 isInTransaction,它告诉被调用的方法是应该自己处理事务,还是让调用者处理事务.

Another approach I've seen used with sqlite in general is to pass in a boolean parameter isInTransaction that tells the called method whether it should handle transactions on its own, or to let the caller handle transactions.

这篇关于SQLiteDatabase 嵌套事务和解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆