英文:
Does mongodb transactions implement optimistic concurrency by default?
问题
MongoDB事务是否默认实现了乐观并发控制?例如,假设我启动一个事务,然后在会话中进行一些更新,在提交之前,该事务中的文档已经被单独更新了。那么该事务是否仍然会执行?
我在考虑如果不执行,我应该在提交之前从数据库中读取正在更新的文档,但我感到有一个缺陷,如果以某种机会文档在提交之前被更新,这将危害数据完整性。
英文:
Hi I am wondering whether mongodb transactions implement optimistic concurrency by default? For example let's say I start a transaction then do some updates in the session and right before I commit an update was made to a document in that transaction seperately. Does that transaction still go through?
I'm thinking if it does not I should read from the database at the documents getting updated before commit but I feel there's a flaw if somehow by some chance the document gets updated before the commit which would compromise data integrity.
答案1
得分: 1
这似乎是答案,MongoDB 在事务中请求更新文档时会锁定该文档。如果在提交之前发生非事务性更新,它将等待事务提交、中止或超时,然后在事务之后进行更新。
英文:
Found the answer to this seems like mongodb locks the document once you request to update it in a transaction. If a non-transactional update happens before the commit it will wait until the transaction is commited, aborted or times out then it will update right after the transaction.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论