강의

멘토링

로드맵

Inflearn Community Q&A

m1108super7485's profile image
m1108super7485

asked

mongoDB from basics to practice (feat. Node.js)

Applying Transaction

session.abortTransaction()에 대한 실제 예시 문의

Resolved

Written on

·

267

0

 

아래 명령어를 통해 transaction이 실패했을 때 원복한다고 하는데, 저런 것은 catch문에 넣어야 하는 것이 맞나요? 아니면 if else로 문제점을 발견했을 때 처리하게 하는 걸까요?

즉, 저 코드를 실제로 사용할 때, 어떤 모습으로 들어가는 지 궁금합니다.

session.abortTransaction()
javascriptnode.jsawsmongodbrest-apidbms/rdbms데이터-엔지니어링

Answer 1

0

sihoon님의 프로필 이미지
sihoon
Instructor

catch가 아니라 try안에 있어야 합니다. 그리고 abortTransaction 대신에 throw를 하셔도 됩니다. 그러면 catch에서 throw한 에러를 받을 수 있어요. 이 경우에도 트렌젝션 안에서 발생한 데이터 수정은 동일하게 복구 되고요.

await session.withTransaction(async () => {
 try {
 // data_update1

 await session.abortTransactio() // or throw new Error("error message")

 // data_update2
 } catch(err){
 
 } finally {
   await session.endSession()
 }
})

 

 

 

m1108super7485's profile image
m1108super7485

asked

Ask a question