Hi:
I was using reactive-stream to write a simple java program to perform the following actions with transaction:
start a new transaction
insert one document to a collection using InsertOneModel()
if the insert fail with E1100 error, then try to update the existing document with new docment using UpdateOneModel()
commit
When I ran my test program, I got the following error at step 3:
reactive stream code with error "Command failed with error 251 (NoSuchTransaction): 'Transaction with { txnNumber: 1 } has been aborted
Does the E1100 cause the existing transaction to be aborted automatically? I know I can use upsert option, but is that the only solution to my current problem?
Errors in server-side operations, such as the DuplicateKeyError, can end the transaction and result in a command error to alert the user that the transaction has ended. This behavior is expected and occurs even if the client never calls Session.abortTransaction(). To incorporate custom error handling, use the Core API on your transaction.
Thank you for the clarification/confirmation. Since the error is from server side and the transaction has been closed (with rollback I assume) by the server, so I will have to repeat my transaction again on my code to handle this error, right?
Yes, I believe upsert is the best solution in my case since I don’t want an E1100 error to close the transaction which contains other operations that I don’t want to repeat (in transaction retry logic) or lose.