Docs 菜单
Docs 主页
/ / /
C#/ .NET驱动程序
/

事务中的批量操作

在本指南中,您可以了解如何使用 MongoDB .NET/C# 驱动程序来执行事务事务允许您运行一系列操作,这些操作在提交事务之前不会更改任何数据。 如果事务中的任何操作返回错误,驱动程序就会取消事务,并在所有数据更改变得可见之前将其丢弃。

MongoDB 保证事务操作中涉及的数据保持一致,即使操作遇到意外错误。

在MongoDB中,事务在逻辑会话中运行。会话是您打算按顺序运行的一组相关读取或写入操作。会话启用一群组操作的因果一致性,或者允许您在ACID transaction中执行操作。

使用 .NET/C# 驱动程序时,您可以从 MongoClient实例创建一个新会话,并将其类型定义为IClientSession 。 我们建议您将客户端重复用于多个会话和事务,而不是每次都实例化一个新客户端。

以下示例展示了如何通过调用 StartSession() 方法来创建会话:

var client = new MongoClient("mongodb://localhost:27017");
var session = client.StartSession();

警告

仅应将 IClientSession 与创建它的 MongoClient(或关联的 MongoDatabaseMongoCollection)一起使用。将 IClientSession 与其他 MongoClient 一起使用会导致操作错误。

您可以通过将 ClientSessionOptions 类的实例传递给 StartSession() 方法来自定义会话行为。下表描述了可以在 ClientSessionOptions对象上设立的属性:

属性
说明

CausalConsistency

Specifies whether the session is causally consistent. In a causally consistent session, the driver executes operations in the order they were issued. To learn more, see Causal Consistency.

Data Type: boolean
Default: true

DefaultTransactionOptions

Specifies the default transaction options for the session. This includes the maximum commit time, read concern, read preference, and write concern.

Default: null

Snapshot

Specifies whether the driver performs snapshot reads. To learn more about snapshot reads, see Read Concern "snapshot" in the MongoDB Server manual.

Data Type: boolean
Default: false

以下代码示例展示了如何使用自定义选项创建会话:

var client = new MongoClient("mongodb://localhost:27017");
var sessionOptions = new ClientSessionOptions
{
CausalConsistency = true,
DefaultTransactionOptions = new TransactionOptions(
readConcern: ReadConcern.Available,
writeConcern: WriteConcern.Acknowledged)
};
var session = client.StartSession(sessionOptions);

MongoDB在某些客户端会话中实现因果一致性。因果一致性模型ACID 一致性保证在分布式系统中,会话中的操作按因果顺序运行。客户端观察到的结果与因果关系或操作之间的依赖关系一致。示例,如果您执行一系列操作,其中一个操作在逻辑上依赖于另一个操作的结果,则任何后续读取都会反映这种依赖关系。

为了保证因果一致性,客户端端会话必须满足以下要求:

  • 启动会话时,驾驶员必须启用因果一致性选项。该选项默认启用。

  • 操作必须在单个线程的单个会话中运行。否则,会话或线程必须相互传达optime和集群时间值。 要查看传达这些值的两个会话的示例,请参阅MongoDB Server手册中的因果一致性示例

  • 您必须使用 ReadConcern.Majority读关注(read concern)。

  • 您必须使用 WriteConcern.WMajority写关注(write concern)。这是默认的写关注(write concern)值。

下表描述了因果一致会话提供的ACID 一致性保证:

保证
说明

读取写入操作

读取操作会反映之前写入操作的结果。

单调读取

读取操作不会返回反映比先前读取操作更早的数据状态的结果。

单调写入

如果写入操作必须先于其他写入操作,则服务器会先运行此写入操作。

示例,如果调用 InsertOne() 插入文档,然后调用 UpdateOne() 修改插入的文档,则服务器首先运行插入操作。

读取后写入

如果写入操作必须在其他读取操作之后执行,服务器会先执行读取操作。

示例,如果您调用 Find() 来检索文档,然后调用 DeleteOne() 来删除检索到的文档,则服务器会先运行查找操作。

提示

要学习;了解有关本节中提到的概念的更多信息,请参阅以下MongoDB Server手册条目:

MongoClient实例使用同步StartSession()或异步StartSessionAsync()方法,创建IClientSession 。 然后,您可以使用IClientSession接口提供的方法集修改会话状态。 从以下 Synchronous MethodsAsynchronous Methods标签页中进行选择,以了解管理事务的方法:

方法
说明

StartTransaction()

Starts a new transaction, configured with the given options, on this session. Throws an exception if there is already a transaction in progress for the session. To learn more about this method, see the startTransaction() page in the Server manual.

Parameter: TransactionOptions (optional)

AbortTransaction()

Ends the active transaction for this session. Throws an exception if there is no active transaction for the session or the transaction has been committed or ended. To learn more about this method, see the abortTransaction() page in the Server manual.

Parameter: CancellationToken

CommitTransaction()

Commits the active transaction for this session. Throws an exception if there is no active transaction for the session or if the transaction was ended. To learn more about this method, see the commitTransaction() page in the Server manual.

Parameter: CancellationToken

WithTransaction()

Starts a transaction on this session and runs the given callback. To learn more about this method, see the withTransaction() page in the Server manual.

重要提示:在 WithTransaction()使用的回调函数中捕获异常时,必须在退出 try-catch区块之前重新抛出异常。否则可能会导致无限循环。 有关如何处理这种情况下的异常的更多详细信息,请参阅《服务器》手册中的“事务”,并从语言下拉列表中选择 C#以查看示例。


Parameters: Func <IClientSessionHandle, CancellationToken, Task<TResult>>, TransactionOptions, CancellationToken
Return Type: Task <TResult>
方法
说明

StartTransaction()

Starts a new transaction, configured with the given options, on this session. Throws an exception if there is already a transaction in progress for the session. To learn more about this method, see the startTransaction() page in the Server manual.

Parameter: TransactionOptions (optional)

AbortTransactionAsync()

Ends the active transaction for this session. Throws an exception if there is no active transaction for the session or the transaction has been committed or ended. To learn more about this method, see the abortTransaction() page in the Server manual.

Parameter: CancellationToken
Return Type: Task

CommitTransactionAsync()

Commits the active transaction for this session. Throws an exception if there is no active transaction for the session or if the transaction was ended. To learn more about this method, see the commitTransaction() page in the Server manual.

Parameter: CancellationToken
Return Type: Task

WithTransactionAsync()

Starts a transaction on this session and runs the given callback. To learn more about this method, see the withTransaction() page in the Server manual.

重要提示:在 WithTransactionAsync()使用的回调函数中捕获异常时,必须在退出 try-catch区块之前重新抛出异常。否则可能会导致无限循环。 有关如何处理这种情况下的异常的更多详细信息,请参阅《服务器》手册中的“事务”,并从语言下拉列表中选择 C#以查看示例。


Parameters: Func <IClientSessionHandle, CancellationToken, Task<TResult>>, TransactionOptions, CancellationToken
Return Type: Task <TResult>

此示例展示了如何通过以下步骤创建会话、创建事务以及将文档插入到事务中的多个集合中:

  1. 使用 StartSession() 方法从客户端创建会话。

  2. 使用 StartTransaction() 方法启动事务。

  3. 将文档插入 booksfilms 集合。

  4. 使用 CommitTransaction() 方法提交事务。

var books = database.GetCollection<Book>("books");
var films = database.GetCollection<Film>("films");
// Begins transaction
using (var session = mongoClient.StartSession())
{
session.StartTransaction();
try
{
// Creates sample data
var book = new Book
{
Title = "Beloved",
Author = "Toni Morrison",
InStock = true
};
var film = new Film
{
Title = "Star Wars",
Director = "George Lucas",
InStock = true
};
// Inserts sample data
books.InsertOne(session, book);
films.InsertOne(session, film);
// Commits our transaction
session.CommitTransaction();
}
catch (Exception e)
{
Console.WriteLine("Error writing to MongoDB: " + e.Message);
return;
}
// Prints a success message if no error thrown
Console.WriteLine("Successfully committed transaction!");
}
Successfully committed transaction!

注意

不支持并行操作

.NET/ C#驱动程序不支持在单个ACID 事务中运行并行操作。

如果您使用的是MongoDB Server v8.0 或更高版本,则可以使用 BulkWrite()BulkWriteAsync() 方法在单个ACID 事务中对多个命名空间执行写入操作。 有关更多信息,请参阅批量写入操作。

要了解有关本指南中提到的概念的更多信息,请参阅服务器手册中的以下页面:

要进一步了解本指南所讨论的任何类型或方法,请参阅以下 API 文档:

后退

批量写入操作

在此页面上