作者:我只是狼却有幅羊的心肠_152 | 来源:互联网 | 2023-05-20 14:36
在我的应用程序中,我调用了一些Web服务来更新数据库。每个Web服务调用都是在特定线程中进行的,从而导致多个线程“一次”更新数据库对象。
在每个线程中,我都使用类似的事务:
Thread 1 (webservice 1)
beginTransaction()
insert a row in the table 1
update a row in the table 1
endTransaction()
Thread 2 (webservice 2)
beginTransaction()
update a row in the table 2
update a row in the table 2
endTransaction()
但是我遇到一个问题,在谷歌搜索后我无法回答自己。由于事务是在不同的线程中处理的,它们是否彼此不同?换句话说,数据库使用的是单独的“声明堆栈”还是公共的?
从我的阅读中,我了解到事务在同一堆栈中,即在线程1中提交事务可能会提交表2上的更新。例如,我认为可能发生的DB语句堆栈:
beginTransaction() //Thread 1 begins a transaction
insert a row in the table 1
update a row in the table 1
beginTransaction() //Thread 2 begins a transaction
update a row in the table 2
update a row in the table 2
endTransaction() //Thread 2 ends a transaction
endTransaction() //Thread 1 ends a transaction
如果是真的,我该如何使我的交易真正排他?我是否必须BEGIN EXCLUSIVE TRANSACTION
在SQLITE_BUSY
任何地方都应对错误,还是有更简单的方法?