개요
이 가이드 에서는 Ruby 운전자 사용하여 트랜잭션을 수행하는 방법을 학습 수 있습니다. 트랜잭션을 사용하면 전체 트랜잭션 이 커밋된 경우에만 데이터를 변경하는 일련의 작업을 수행할 수 있습니다. 트랜잭션 의 작업이 성공하지 못하면 운전자 트랜잭션 중지하고 데이터 변경 사항이 표시되기 전에 모든 데이터 변경 사항을 삭제합니다. 이 기능 원자성 이라고 합니다.
MongoDB 에서 트랜잭션은 논리적 세션 내에서 실행 . 세션은 순차적으로 실행 하려는 관련 읽기 또는 쓰기 (write) 작업의 그룹입니다. 세션을 사용하면 작업 그룹 에 인과적 일관성 활성화 원자성, 일관성, 격리 및 내구성에 대한 기대치를 충족하는 트랜잭션 인 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
메서드를 정의합니다. 이 코드는 다음 조치를 수행합니다.
Mongo::Collection
인스턴스를 생성하여movies
및users
컬렉션에 액세스 .트랜잭션 에 대한 읽기 및 쓰기 (write) 고려를 지정합니다.
트랜잭션 시작합니다.
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
메서드를 사용하여 단일 트랜잭션 내에서 여러 네임스페이스에 대한 쓰기 (write) 작업을 수행할 수 있습니다. 자세한 내용은 대량 쓰기 작업 가이드 참조하세요.
추가 정보
이 가이드 에 언급된 개념에 학습 보려면 MongoDB Server 매뉴얼의 다음 페이지를 참조하세요.
ACID compliance 에 대해 자세히 학습 MongoDB 웹사이트 에서 A Guide to ACID Properties in Database Management Systems(데이터베이스 관리 시스템의 ACID 속성 가이드) 문서를 참조하세요.
삽입 작업에 대해 자세히 알아보려면 문서 삽입 가이드를 참조하세요.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 문서를 참조하세요.