Overview
このガイドでは、 MongoDB .NET/ C#ドライバーを使用してトランザクションを実行する方法を学習できます。 トランザクションを使用すると、トランザクションがコミットされるまでデータを変更しない一連の操作を実行できます。トランザクション内のいずれかの操作でエラーが返された場合、ドライバーはトランザクションをキャンセルし、変更が反映される前にすべてのデータ変更を破棄します。
MongoDB では、トランザクションは論理セッション内で実行されます。セッションは、順番に実行されるよう関連付けられた読み取り操作または書き込み操作のグループです。セッションにより、一連の操作に対する因果整合性が有効になる、または ACID transaction 内で操作を実行できるようになります。MongoDBは、トランザクション操作で予期せぬエラーが発生した場合でも、その操作に関わるデータの一貫性が保たれることを保証します。
.NET/C# ドライバーを使用すると、 MongoClientインスタンスからIClientSession型の新しいセッションを作成できます。 毎回新しいクライアントをインスタンス化するのではなく、クライアントを複数のセッションやトランザクションで再利用することを推奨します。
警告
IClientSessionは、それを作成したMongoClient (または関連付けられたMongoDatabaseまたはMongoCollection )でのみ使用します。 IClientSessionと別のMongoClientを使用すると、操作エラーが発生します。
メソッド
IClientSessionインスタンスで同期StartSession() メソッドまたは非同期StartSessionAsync() メソッドのいずれかを使用してMongoClient を作成します。次に、 IClientSessionインターフェースが提供するメソッドセットを使用してセッション状態を変更します。 トランザクションを管理する方法については、次の Synchronous Methodsタブと タブから選択してください。Asynchronous Methods
方式 | 説明 |
|---|---|
| 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. |
| 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. |
| 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. |
| 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. IMPORTANT: When catching exceptions within the callback function used by
|
方式 | 説明 |
|---|---|
| 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. |
| 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. |
| 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. |
| 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. IMPORTANT: When catching exceptions within the callback function used by
|
トランザクションのオプション
個別のトランザクションを構成するには、TransactionOptionsインスタンスを StartTransaction() メソッドまたは WithTransaction() メソッドに渡します。このセクションの例では、読み取り保証 (read concern)を ReadConcern.Majority に設定し、書込み保証 (write concern)をWriteConcern.WMajority に設定します。
次のプロパティを持つ TransactionOptionsオブジェクトを構成できます。
プロパティ | 説明 |
|---|---|
| Maximum amount of time that a single |
| Read concern for the transaction. To learn more, see Read Concern in the MongoDB Server manual. |
| Read preference for the transaction. To learn more, see Read Preference in the MongoDB Server manual. |
| Write concern for the transaction. To learn more, see Write Concern in the MongoDB Server manual. |
例
この例では、次の手順でセッションを作成し、トランザクションオプションを構成し、トランザクションを作成し、トランザクション内の複数のコレクションにドキュメントを挿入する方法を示します。
StartSession()メソッドを使用してクライアントからセッションを作成します。トランザクションを構成するには、
TransactionOptionsオブジェクトを作成します。トランザクションを開始するには、
StartTransaction()メソッドを使用します。booksfilmsコレクションと コレクションにドキュメントを挿入します。CommitTransaction()メソッドを使用してトランザクションをコミットします。
var books = database.GetCollection<Book>("books"); var films = database.GetCollection<Film>("films"); // Begins transaction using (var session = mongoClient.StartSession()) { // Configures transaction options var transactionOptions = new TransactionOptions( readConcern: ReadConcern.Majority, writeConcern: WriteConcern.WMajority ); session.StartTransaction(transactionOptions); 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!"); }
注意
並列操作はサポートされていません
.NET/ C#ドライバーは、単一のトランザクション内での並列操作の実行中をサポートしていません。
詳細情報
このガイドで言及されている概念の詳細については、サーバー マニュアルの次のページを参照してください。
API ドキュメント
このガイドで説明した型やメソッドの詳細については、次の API ドキュメントを参照してください。