MongoDB 4.0事务:ACID读写? [英] MongoDB 4.0 Transactions: ACID Read + Write?

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

问题描述

因此MongoDB 4.0附带了多文档事务.我的问题是,这是否启用了与SQL过程相同的功能?

So MongoDB 4.0 comes with multi document transactions. My question is, does this enable the same functionality you get with SQL Procedures?

用例为

  1. 锁定收藏
  2. 从收藏集中阅读
  3. 根据2的结果写入收藏集.
  4. 提交/中止

常见的数据库驱动程序通常只会堆积您发出的所有命令,直到您调用commit,然后在数据库计算机上依次运行它们.因此,我在服务器代码中运行的所有读取操作都是在实际提交事务之前进行的,因此其他连接可能会在读取和写入操作之间更改数据.

Common db drivers will usually just stack up any commands you issue until you call commit and then run them all after another on the db's machine. So any read I run in my server code are run before the transaction is actually commited and therefore other connections could alter data in between read and write operations.

MongoDB 4.0将涵盖此功能吗?

Will MongoDB 4.0 cover this functionality?

推荐答案

MongoDB 4.0将涵盖此功能吗?

Will MongoDB 4.0 cover this functionality?

对于原子性,简短的答案是肯定的.

The short answer is yes for atomicity.

在MongoDB中,交易(也称为多文档交易)与会话.也就是说,您开始一个会话的事务.在任何给定时间,一个会话最多可以有一个 个开放交易.

In MongoDB, Transactions (also called multi-documents transactions) are associated with a session. That is, you start a transaction for a session. At any given time, you can have at most one open transaction for a session.

您不能锁定整个集合进行写操作.您可能需要创建多个事务,以确保进程之间的写入不会交错/覆盖. MongoDB使用乐观锁定而不是

You can not lock the entire collection for writes. You may want to create multiple transactions to ensure that writes are not interlacing/overriding between your processes. MongoDB uses Optimistic Locking instead of Pessimistic Locking.

因此,我在服务器代码中运行的所有读取操作均在实际提交事务之前进行,因此其他连接可能会在读取和写入操作之间更改数据

So any read I run in my server code are run before the transaction is actually commited and therefore other connections could alter data in between read and write operations

类似地,在MongoDB中进行多文档事务处理.例如,使用 mongo shell :

Similarly in MongoDB multi-document transactions. For example, using mongo shell:

s1 = Mongo().startSession() 
sessionTest = s1.getDatabase("databaseName").test;
s1.startTransaction() 
sessionTest.find({a:"foo"})
> {_id: ObjectId(..), a:"foo", b:1}

// Let's update the record outside of the session (i.e. another process)
db.test.update({a:"foo"}, {$set:{b:2}})

sessionTest.update({a:"foo"}, {$set:{b:9}})
// You'll get a WriteConflict error because the the document has been modified outside of the session. 

还要注意,当事务打开时,在事务外部看不到事务中的操作进行的任何数据更改.

Also note that while the transaction is open, no data changes made by operations in the transaction is visible outside of the transaction.

  • 提交事务时,所有数据更改都将保存并在事务外部可见,并且事务结束.
  • 当事务中止时,由该事务中的写入所做的所有数据更改都将被丢弃,而不会变得可见,并且该事务结束.

另请参见原子性示例.

值得一提的是,MongoDB是分布式数据库,因此您还需要注意一致性的不同选择.您可以在启动 Session.startTransaction()时指定这些选项. a>取决于使用情况:

Worth noting that MongoDB is a distributed database, so you also need to be aware of the different options for consistency. You can specify these options when initiating Session.startTransaction() depending on the use case :

  • Read Isolation (Read Concern): MongoDB multi-document transactions support read concern "snapshot", "local", and "majority".

写确认(写关注).

多文档交易支持读取首选项主要和所有操作在给定的交易中,必须路由到同一成员.

Multi-document transactions support read preference primary and all operations in a given transaction must route to the same member.

您可能还对工程粉笔和讲座:MongoDB交易视频感兴趣,这些视频包含一些背后的技术说明MongoDB事务.

You may also be interested in Engineering Chalk and Talks: MongoDB Transactions videos which contain some technical explanations behind MongoDB transactions.

这篇关于MongoDB 4.0事务:ACID读写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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