Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Ruby ドライバー
/

トランザクション

このガイドでは、 Rubyドライバーを使用して トランザクション を実行する方法を学習できます。トランザクションを使用すると、トランザクション全体がコミットされた場合にのみ、データを変更する一連の操作を実行できます。トランザクション内のいずれかの操作が成功しない場合、ドライバーはトランザクションを停止し、変更が反映される前にすべてのデータ変更を破棄します。この特徴は アトミック性と 呼ばれます。

MongoDBでは、トランザクションは論理 セッション 内で実行されます。セッションは 、順番に実行されるよう関連付けられた読み取り操作または書き込み操作のグループです。 セッションにより、一連の操作に対する 因果整合性 が有効になり、 ACID準拠のトランザクション内で操作を実行できるようになります。これは、アトミック性、整合性、分離、 耐久性 の期待を満たすトランザクションです。MongoDBは、トランザクション操作で予期せぬエラーが発生した場合でも、その操作に関わるデータの一貫性が保たれることを保証します。

Rubyドライバーを使用している場合、クライアントで start_session メソッドを呼び出すことでセッションを開始できます。その後、セッション内でトランザクションを実行できるようになります。

警告

セッションは、セッションを作成した Mongo::Client で実行中操作でのみ使用します。異なる Mongo::Client を持つセッションを使用すると、操作エラーが発生します。

start_session メソッドを呼び出してセッションを開始した後、Mongo::Sessionクラスのメソッドを使用してセッション状態を管理できます。次の表では、トランザクションを管理するために使用できる方法について説明します。

方式
説明

start_transaction

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.

commit_transaction

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.

abort_transaction

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.

with_transaction

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ドキュメントを参照してください。

戻る

一括書き込み操作

項目一覧