原子性和事务

1 在单个文档修改多个嵌入文档,写操作都在文档级别上都是原子的

2 在单个写操作修改多个文档时,每个文档的修改都具有原子性,但是,作为一个整体的操作,并不是原子的。其他操作可能有交互。使用$isolated操作符隔离单个写操作,可以影响多个文档

3 使用$isolated操作符,影响多个文档的写操作可以防止其他进程对其在修改操作过程中进行交互,在这样就确保了在写操作完成或者发生错误之前,没有客户端能看到这些更改

4 $isolated在分片集群中不支持

5 $isolated并不会有全有或者全无的概念,也就是说当你异常发生之时,之前的操作并不具有回滚的特性

6 $isolated会对集合有一个独占锁的操作,即使是像wiredTiger这种文档级锁定存储引擎也是一样,也就是说,在操作期间,$isolated使wiredTiger单线程化了

7 单个文档可以包含多个嵌入式文档,单文档的原子性在多个实际用例中已经足够,对于一个写操作序列则必须像在单个事务中那样操作的情况,你可以在用用程序中使用二阶段提交

In MongoDB, a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.

When a single write operation modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic and other operations may interleave. However, you can isolate a single write operation that affects multiple documents using the $isolated operator.

$isolated Operator

Using the $isolated operator, a write operation that affects multiple documents can prevent other processes from interleaving once the write operation modifies the first document. This ensures that no client sees the changes until the write operation completes or errors out.

$isolated does not work with sharded clusters.

An isolated write operation does not provide “all-or-nothing” atomicity. That is, an error during the write operation does not roll back all its changes that preceded the error.

NOTE

$isolated operator causes write operations to acquire an exclusive lock on the collection, even for document-level locking storage engines such as WiredTiger. That is, $isolated operator will make WiredTiger single-threaded for the duration of the operation.

For an example of an update operation that uses the $isolated operator, see $isolated. For an example of a remove operation that uses the $isolated operator, see Isolate Remove Operations.

Transaction-Like Semantics

Since a single document can contain multiple embedded documents, single-document atomicity is sufficient for many practical use cases. For cases where a sequence of write operations must operate as if in a single transaction, you can implement a two-phase commit in your application.

However, two-phase commits can only offer transaction-like semantics. Using two-phase commit ensures data consistency, but it is possible for applications to return intermediate data during the two-phase commit or rollback.

For more information on two-phase commit and rollback, see Perform Two Phase Commits.

Concurrency Control

Concurrency control allows multiple applications to run concurrently without causing data inconsistency or conflicts.

One approach is to create a unique index on a field that can only have unique values. This prevents insertions or updates from creating duplicate data. Create a unique index on multiple fields to force uniqueness on that combination of field values. For examples of use cases, see update() and Unique Index and findAndModify() and Unique Index.

Another approach is to specify the expected current value of a field in the query predicate for the write operations. The two-phase commit pattern provides a variation where the query predicate includes theapplication identifier as well as the expected state of the data in the write operation.

 

posted on 2017-08-18 11:28  懒睡的猫熊  阅读(337)  评论(0编辑  收藏  举报