https://docs.mongodb.com/manual/core/transactions-in-applications
MongoDB 4.x 부터는 Transaction 처리가 가능한데, 기존 rdbms 처럼 Transaction 처리 시 접할 수 있는 문제가 아닌(transaction 처리 시 성공으로 인한 commit / 실패로 인한 rollback 개념이 아닌) 개발 단에서 확인되는 에러 경험담을 공유합니다.
MongoDB API 를 이용하여 Transaction 처리를 시도하는데 (코드에서) transaction 처리 완료 시 commit 진행을 하는데, 이 때 에러가 발생할 수 있습니다. 이러한 에러는 단순 한번 테스트에서 발생하는 것이 아닌, 벤치마킹 같이 여러 connection 에서 시도를 하다가 발생할 수 있는데, 에러는 총 2가지 종류가 있습니다.
첫 번째로는 "TransientTransactionError" 로 해당 에러의 경우 Transaction 전체를 다시 시도하라는 에러로써, 이러한 에러가 발생이 되면, Transaction 전체 내용을 다시 처음부터 시도 해야 합니다.
두 번째로는 "UnknownTransactionCommitResult" 로써, 해당 에러의 경우 Transaction 의 마지막 commit 부분만 다시 시도하라는 것입니다. 이러한 에러가 발생이 되면 마지막 commit 만 다시 시도 하면 처리가 가능 합니다.
단 한번으로 끝나는게 아니라, 이러한 내용도 하나의 session 에서 여러번 마주칠 수 있기 때문에...(실패의 재시도 -> 실패 - >재시도...) while 로 처리 또는 exception 처리가 필요합니다.
Regardless of the database system, whether MongoDB or relational databases, applications should take measures to handle errors during transaction commits and incorporate retry logic for transactions.
"TransientTransactionError" <-- 해당 내용은 Transaction 처리하는 내용 전체를 다시 시도
The individual write operations inside the transaction are not retryable, regardless of the value of retryWrites. If an operation encounters an error associated with the label "TransientTransactionError", such as when the primary steps down, the transaction as a whole can be retried.
"UnknownTransactionCommitResult" <-- 해당 내용은 Commit 부분만 다시 시도 요청
The commit operations are retryable write operations. If the commit operation encounters an error, MongoDB drivers retry the commit regardless of the value of retryWrites.
If the commit operation encounters an error labeled "UnknownTransactionCommitResult", the commit can be retried.