123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.isTransactionCommand = exports.Transaction = exports.TxnState = void 0;
- const error_1 = require("./error");
- const read_concern_1 = require("./read_concern");
- const read_preference_1 = require("./read_preference");
- const write_concern_1 = require("./write_concern");
- exports.TxnState = Object.freeze({
- NO_TRANSACTION: 'NO_TRANSACTION',
- STARTING_TRANSACTION: 'STARTING_TRANSACTION',
- TRANSACTION_IN_PROGRESS: 'TRANSACTION_IN_PROGRESS',
- TRANSACTION_COMMITTED: 'TRANSACTION_COMMITTED',
- TRANSACTION_COMMITTED_EMPTY: 'TRANSACTION_COMMITTED_EMPTY',
- TRANSACTION_ABORTED: 'TRANSACTION_ABORTED'
- });
- const stateMachine = {
- [exports.TxnState.NO_TRANSACTION]: [exports.TxnState.NO_TRANSACTION, exports.TxnState.STARTING_TRANSACTION],
- [exports.TxnState.STARTING_TRANSACTION]: [
- exports.TxnState.TRANSACTION_IN_PROGRESS,
- exports.TxnState.TRANSACTION_COMMITTED,
- exports.TxnState.TRANSACTION_COMMITTED_EMPTY,
- exports.TxnState.TRANSACTION_ABORTED
- ],
- [exports.TxnState.TRANSACTION_IN_PROGRESS]: [
- exports.TxnState.TRANSACTION_IN_PROGRESS,
- exports.TxnState.TRANSACTION_COMMITTED,
- exports.TxnState.TRANSACTION_ABORTED
- ],
- [exports.TxnState.TRANSACTION_COMMITTED]: [
- exports.TxnState.TRANSACTION_COMMITTED,
- exports.TxnState.TRANSACTION_COMMITTED_EMPTY,
- exports.TxnState.STARTING_TRANSACTION,
- exports.TxnState.NO_TRANSACTION
- ],
- [exports.TxnState.TRANSACTION_ABORTED]: [exports.TxnState.STARTING_TRANSACTION, exports.TxnState.NO_TRANSACTION],
- [exports.TxnState.TRANSACTION_COMMITTED_EMPTY]: [
- exports.TxnState.TRANSACTION_COMMITTED_EMPTY,
- exports.TxnState.NO_TRANSACTION
- ]
- };
- const ACTIVE_STATES = new Set([
- exports.TxnState.STARTING_TRANSACTION,
- exports.TxnState.TRANSACTION_IN_PROGRESS
- ]);
- const COMMITTED_STATES = new Set([
- exports.TxnState.TRANSACTION_COMMITTED,
- exports.TxnState.TRANSACTION_COMMITTED_EMPTY,
- exports.TxnState.TRANSACTION_ABORTED
- ]);
- class Transaction {
-
- constructor(options) {
- options = options ?? {};
- this.state = exports.TxnState.NO_TRANSACTION;
- this.options = {};
- const writeConcern = write_concern_1.WriteConcern.fromOptions(options);
- if (writeConcern) {
- if (writeConcern.w === 0) {
- throw new error_1.MongoTransactionError('Transactions do not support unacknowledged write concern');
- }
- this.options.writeConcern = writeConcern;
- }
- if (options.readConcern) {
- this.options.readConcern = read_concern_1.ReadConcern.fromOptions(options);
- }
- if (options.readPreference) {
- this.options.readPreference = read_preference_1.ReadPreference.fromOptions(options);
- }
- if (options.maxCommitTimeMS) {
- this.options.maxTimeMS = options.maxCommitTimeMS;
- }
-
- this._pinnedServer = undefined;
- this._recoveryToken = undefined;
- }
-
- get server() {
- return this._pinnedServer;
- }
- get recoveryToken() {
- return this._recoveryToken;
- }
- get isPinned() {
- return !!this.server;
- }
-
- get isStarting() {
- return this.state === exports.TxnState.STARTING_TRANSACTION;
- }
-
- get isActive() {
- return ACTIVE_STATES.has(this.state);
- }
- get isCommitted() {
- return COMMITTED_STATES.has(this.state);
- }
-
- transition(nextState) {
- const nextStates = stateMachine[this.state];
- if (nextStates && nextStates.includes(nextState)) {
- this.state = nextState;
- if (this.state === exports.TxnState.NO_TRANSACTION ||
- this.state === exports.TxnState.STARTING_TRANSACTION ||
- this.state === exports.TxnState.TRANSACTION_ABORTED) {
- this.unpinServer();
- }
- return;
- }
- throw new error_1.MongoRuntimeError(`Attempted illegal state transition from [${this.state}] to [${nextState}]`);
- }
-
- pinServer(server) {
- if (this.isActive) {
- this._pinnedServer = server;
- }
- }
-
- unpinServer() {
- this._pinnedServer = undefined;
- }
- }
- exports.Transaction = Transaction;
- function isTransactionCommand(command) {
- return !!(command.commitTransaction || command.abortTransaction);
- }
- exports.isTransactionCommand = isTransactionCommand;
|