gpt4 book ai didi

android - 如果嵌套事务失败,父事务也会失败吗

转载 作者:行者123 更新时间:2023-11-30 02:59:36 29 4
gpt4 key购买 nike

我有嵌套交易的代码

db.beginTransaction();
try {
deleteCustomer();
insert_customer_function();

// No exception throw. Success
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}

void insert_customer_function() {
db.beginTransaction();
try {
insertCustomer();
// Exception throw somehow...
db.setTransactionSuccessful();
} catch (Exception ex) {
} finally {
db.endTransaction();
}
}

我想知道,如果嵌套事务 insert_customer_function 失败,deleteCustomer 会被提交还是被回滚?

来自 https://www.sqlite.org/lang_savepoint.html , 好像是

Note that an inner transaction might commit (using the RELEASE command) but then later have its work undone by a ROLLBACK in an outer transaction. A power failure or program crash or OS crash will cause the outer-most transaction to rollback, undoing all changes that have occurred within that outer transaction, even changes that have supposedly been "committed" by the RELEASE command. Content is not actually committed on the disk until the outermost transaction commits.

好像是

  • 如果外部不提交,即使内部提交,外部和内部都将回滚。

但是如果,

  • 如果外层提交,但内层不提交,是否会回滚外层和内层?

最佳答案

如果被调用的方法失败,您可以抛出异常,这样调用方就会出错,“父”事务也会回滚。

通过抛出一个异常,你强制一个错误,然后将错误传播回调用者,调用者在 try catch block 中处理它。

快速引用:http://androidcookbook.com/Recipe.seam;jsessionid=0443546CEE776318BF6D21552A9D1864?recipeId=75&recipeFrom=ViewTOC

关于android - 如果嵌套事务失败,父事务也会失败吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747213/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com