* Starts a new transaction with the given options. * * @remarks * **IMPORTANT**: Running operations in parallel is not supported during a transaction. The use of `Promise.all`, * `Promise.allSettled`, `Promise.race`, etc to parallelize operations inside a transaction is * undefined be
(options?: TransactionOptions)
| 386 | * @param options - Options for the transaction |
| 387 | */ |
| 388 | startTransaction(options?: TransactionOptions): void { |
| 389 | if (this.snapshotEnabled) { |
| 390 | throw new MongoCompatibilityError('Transactions are not supported in snapshot sessions'); |
| 391 | } |
| 392 | |
| 393 | if (this.inTransaction()) { |
| 394 | throw new MongoTransactionError('Transaction already in progress'); |
| 395 | } |
| 396 | |
| 397 | if (this.isPinned && this.transaction.isCommitted) { |
| 398 | this.unpin(); |
| 399 | } |
| 400 | |
| 401 | this.commitAttempted = false; |
| 402 | // increment txnNumber |
| 403 | this.incrementTransactionNumber(); |
| 404 | // create transaction state |
| 405 | this.transaction = new Transaction({ |
| 406 | readConcern: |
| 407 | options?.readConcern ?? |
| 408 | this.defaultTransactionOptions.readConcern ?? |
| 409 | this.clientOptions?.readConcern, |
| 410 | writeConcern: |
| 411 | options?.writeConcern ?? |
| 412 | this.defaultTransactionOptions.writeConcern ?? |
| 413 | this.clientOptions?.writeConcern, |
| 414 | readPreference: |
| 415 | options?.readPreference ?? |
| 416 | this.defaultTransactionOptions.readPreference ?? |
| 417 | this.clientOptions?.readPreference, |
| 418 | maxCommitTimeMS: options?.maxCommitTimeMS ?? this.defaultTransactionOptions.maxCommitTimeMS |
| 419 | }); |
| 420 | |
| 421 | this.transaction.transition(TxnState.STARTING_TRANSACTION); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Commits the currently active transaction in this session. |