Overview
このガイドでは、 Rubyドライバーを使用して トランザクション を実行する方法を学習できます。トランザクションを使用すると、トランザクション全体がコミットされた場合にのみ、データを変更する一連の操作を実行できます。トランザクション内のいずれかの操作が成功しない場合、ドライバーはトランザクションを停止し、変更が反映される前にすべてのデータ変更を破棄します。この特徴は アトミック性と 呼ばれます。
MongoDBでは、トランザクションは論理 セッション 内で実行されます。セッションは 、順番に実行されるよう関連付けられた読み取り操作または書き込み操作のグループです。 セッションにより、一連の操作に対する 因果整合性 が有効になり、 ACID準拠のトランザクション内で操作を実行できるようになります。これは、アトミック性、整合性、分離、 耐久性 の期待を満たすトランザクションです。MongoDBは、トランザクション操作で予期せぬエラーが発生した場合でも、その操作に関わるデータの一貫性が保たれることを保証します。
Rubyドライバーを使用している場合、クライアントで start_session
メソッドを呼び出すことでセッションを開始できます。その後、セッション内でトランザクションを実行できるようになります。
警告
セッションは、セッションを作成した Mongo::Client
で実行中操作でのみ使用します。異なる Mongo::Client
を持つセッションを使用すると、操作エラーが発生します。
メソッド
start_session
メソッドを呼び出してセッションを開始した後、Mongo::Session
クラスのメソッドを使用してセッション状態を管理できます。次の表では、トランザクションを管理するために使用できる方法について説明します。
方式 | 説明 |
---|---|
| Starts a new transaction on this session. You cannot start a
transaction if there's already an active transaction running in
the session. You can set transaction options including read concern, write concern,
and read preference by passing a Hash as a parameter. |
| Commits the active transaction for this session. This method returns an
error if there is no active transaction for the session, the
transaction was previously ended, or if there is a write conflict. |
| Ends the active transaction for this session. This method returns an
error if there is no active transaction for the session or if the
transaction was committed or ended. |
| Starts a transaction prior to calling the supplied block, and commits
the transaction when the block finishes. If any of the operations in
the block, or the commit operation, result in a transient transaction
error, the block and/or the commit will be executed again. |
トランザクションの例
この例では、 sample_mflix
データベースのコレクション内のデータを変更する run_transaction
メソッドを定義しています。 このコードは、次のアクションを実行します。
movies
コレクションとusers
コレクションにアクセスするためのMongo::Collection
インスタンスを作成します。トランザクションの読み取り保証(read concern)と書込み保証(write concern)を指定します。
トランザクションを開始します。
movies
コレクションにドキュメントを挿入し、結果を出力します。users
コレクション内のドキュメントを更新し、結果を出力します。
database = client.use('sample_mflix') movies_collection = database[:movies] users_collection = database[:users] def run_transaction(session, movies_collection, users_collection) transaction_options = { read_concern: { level: "snapshot" }, write_concern: { w: "majority" } } session.with_transaction(transaction_options) do # Inserts document into the "movies" collection insert_result = movies_collection.insert_one({ name: 'The Menu', runtime: 107 }, session: session) puts "Insert completed: #{insert_result.inspect}" # Updates document in the "users" collection update_result = users_collection.update_one({ name: 'Amy Phillips'}, { "$set" => { name: 'Amy Ryan' }}, session: session) puts "Update completed: #{update_result.inspect}" end end # Starts a session session = client.start_session begin # Runs the transaction run_transaction(session, movies_collection, users_collection) puts "Transaction committed successfully." rescue Mongo::Error::OperationFailure => e puts "Transaction failed and was aborted. Error: #{e.message}" ensure session.end_session end
注意
並列操作はサポートされていません
Rubyドライバーは、単一のトランザクション内での並列操作の実行中をサポートしていません。
MongoDB Server v8.0 以降を使用している場合は、bulk_write
メソッドを使用して、1 つのトランザクション内で複数の名前空間に対して書込み操作を実行できます。 詳細については、 一括書込み操作 のガイドをご覧ください。
詳細情報
このガイドで言及されている概念の詳細については、 MongoDB Serverマニュアルの次のページ を参照してください。
ACID コンプライアンスの詳細については、 MongoDBウェブサイトの「 データベース管理システムのACIDプロパティに関するガイド 」に関する記事を参照してください。
挿入操作の詳細については、ドキュメントの挿入ガイドを参照してください。
API ドキュメント
このガイドで言及されているメソッドとタイプの詳細については、次のAPIドキュメントを参照してください。