Skip to content

Commit d15dcbb

Browse files
docs(NODE-5721): thrown error expectations in withTransaction (#4038)
1 parent 77d0b47 commit d15dcbb

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/sessions.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,26 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
435435
/**
436436
* Starts a transaction and runs a provided function, ensuring the commitTransaction is always attempted when all operations run in the function have completed.
437437
*
438-
* **IMPORTANT:** This method requires the user to return a Promise, and `await` all operations.
438+
* **IMPORTANT:** This method requires the function passed in to return a Promise. That promise must be made by `await`-ing all operations in such a way that rejections are propagated to the returned promise.
439439
*
440440
* @remarks
441-
* This function:
442-
* - If all operations successfully complete and the `commitTransaction` operation is successful, then this function will return the result of the provided function.
443-
* - If the transaction is unable to complete or an error is thrown from within the provided function, then this function will throw an error.
441+
* - If all operations successfully complete and the `commitTransaction` operation is successful, then the provided function will return the result of the provided function.
442+
* - If the transaction is unable to complete or an error is thrown from within the provided function, then the provided function will throw an error.
444443
* - If the transaction is manually aborted within the provided function it will not throw.
445-
* - May be called multiple times if the driver needs to attempt to retry the operations.
444+
* - If the driver needs to attempt to retry the operations, the provided function may be called multiple times.
446445
*
447446
* Checkout a descriptive example here:
448447
* @see https://www.mongodb.com/blog/post/quick-start-nodejs--mongodb--how-to-implement-transactions
449448
*
449+
* If a command inside withTransaction fails:
450+
* - It may cause the transaction on the server to be aborted.
451+
* - This situation is normally handled transparently by the driver.
452+
* - However, if the application catches such an error and does not rethrow it, the driver will not be able to determine whether the transaction was aborted or not.
453+
* - The driver will then retry the transaction indefinitely.
454+
*
455+
* To avoid this situation, the application must not silently handle errors within the provided function.
456+
* If the application needs to handle errors within, it must await all operations such that if an operation is rejected it becomes the rejection of the callback function passed into withTransaction.
457+
*
450458
* @param fn - callback to run within a transaction
451459
* @param options - optional settings for the transaction
452460
* @returns A raw command response or undefined

0 commit comments

Comments
 (0)