MongoDB事务是否默认实现了乐观并发?

huangapple go评论81阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月13日 11:38:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定