Callback API와 Core API
The Callback API:
트랜잭션을 시작하고, 지정된 작업을 실행하며, 커밋(또는 오류 발생 시 중단)합니다.
Automatically incorporates error handling logic for
TransientTransactionErrorandUnknownTransactionCommitResult.
The Core API:
트랜잭션을 시작하고 커밋하려면 명시적인 호출이 필요합니다.
Does not incorporate error handling logic for
TransientTransactionErrorandUnknownTransactionCommitResult, and instead provides the flexibility to incorporate custom error handling for these errors.
Callback API
콜백 API는 로직을 통합합니다.
To retry the transaction as a whole if the transaction encounters a
TransientTransactionErrorerror.To retry the commit operation if the commit encounters an
UnknownTransactionCommitResulterror.
Starting in MongoDB 6.2, the server does not retry the transaction if it receives a TransactionTooLargeForCache error.
예시
Core API
코어 트랜잭션 API에는 다음과 같은 레이블이 지정된 오류에 대한 재시도 로직이 포함되어 있지 않습니다.
TransientTransactionError. If an operation in a transaction returns an error labeledTransientTransactionError, the transaction as a whole can be retried.To handle
TransientTransactionError, applications should explicitly incorporate retry logic for the error.UnknownTransactionCommitResult. If the commit returns an error labeledUnknownTransactionCommitResult, the commit can be retried.To handle
UnknownTransactionCommitResult, applications should explicitly incorporate retry logic for the error.
예시
다음 예시에는 일시적인 오류에 대해 트랜잭션을 다시 시도하고 알 수 없는 커밋 오류에 대해 커밋을 다시 시도하는 로직이 포함되어 있습니다.
드라이버 버전
트랜잭션 오류 처리
데이터베이스 시스템(MongoDB 또는 관계형 데이터베이스)에 관계없이 애플리케이션은 트랜잭션 커밋 중 오류를 처리하고 트랜잭션에 대한 재시도 로직을 통합하는 조치를 취해야 합니다.
TransientTransactionError
트랜잭션 내부의 개별 쓰기 (write) 작업은 값에 관계없이 재시도할 수 retryWrites 없습니다. 프라이머리 머리가 물러나는 "TransientTransactionError" 경우와 같이 작업에서 레이블 과 관련된 오류가 발생하면 트랜잭션 전체를 다시 시도할 수 있습니다.
콜백 API는
"TransientTransactionError"에 대한 재시도 로직을 통합합니다.The core transaction API does not incorporate retry logic for
"TransientTransactionError". To handle"TransientTransactionError", applications should explicitly incorporate retry logic for the error. To view an example that incorporates retry logic for transient errors, see Core API Example.
UnknownTransactionCommitResult
커밋 작업은 재시도 가능한 쓰기 작업 입니다. 커밋 작업에서 오류가 발생하면 retryWrites의 값에 관계없이 MongoDB 드라이버는 커밋을 다시 시도합니다.
커밋 작업에서 "UnknownTransactionCommitResult" 레이블이 지정된 오류가 발생하면 커밋을 다시 시도할 수 있습니다.
콜백 API는
"UnknownTransactionCommitResult"에 대한 재시도 로직을 통합합니다.The core transaction API does not incorporate retry logic for
"UnknownTransactionCommitResult". To handle"UnknownTransactionCommitResult", applications should explicitly incorporate retry logic for the error. To view an example that incorporates retry logic for unknown commit errors, see Core API Example.
TransactionTooLargeForCache
버전 6.2에 추가 되었습니다.
MongoDB 6.2부터 서버는 TransactionTooLargeForCache 오류를 수신하는 경우 트랜잭션을 재시도하지 않습니다. 이 오류는 캐시가 너무 작아 재시도에 실패할 가능성이 높다는 의미입니다.
transactionTooLargeForCacheThreshold 임계값의 기본값은 0.75입니다. 트랜잭션이 캐시의 75%를 초과하여 사용하는 경우, 서버는 트랜잭션을 재시도하는 대신 TransactionTooLargeForCache를 반환합니다.
이전 버전의 MongoDB에서는 서버가 TransactionTooLargeForCache 대신 TemporarilyUnavailable 또는 WriteConflict를 반환합니다.
오류 임계값을 수정하려면 setParameter 명령을 사용합니다.
추가 정보
mongosh 예시
The following mongosh methods are available for transactions:
참고
The mongosh example omits retry logic and robust error handling for simplicity's sake. For a more practical example of incorporating transactions in applications, see Transaction Error Handling instead.
// Create collections: db.getSiblingDB("mydb1").foo.insertOne( {abc: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } ) db.getSiblingDB("mydb2").bar.insertOne( {xyz: 0}, { writeConcern: { w: "majority", wtimeout: 2000 } } ) // Start a session. session = db.getMongo().startSession( { readPreference: { mode: "primary" } } ); coll1 = session.getDatabase("mydb1").foo; coll2 = session.getDatabase("mydb2").bar; // Start a transaction session.startTransaction( { readConcern: { level: "local" }, writeConcern: { w: "majority" } } ); // Operations inside the transaction try { coll1.insertOne( { abc: 1 } ); coll2.insertOne( { xyz: 999 } ); } catch (error) { // Abort transaction on error session.abortTransaction(); throw error; } // Commit the transaction using write concern set at transaction start session.commitTransaction(); session.endSession();