diff options
author | Pavi Vetriselvan <pvselvan@umich.edu> | 2018-05-18 11:33:18 -0400 |
---|---|---|
committer | Pavi Vetriselvan <pvselvan@umich.edu> | 2018-05-18 11:46:06 -0400 |
commit | 4b4f53f64a5b364b74c92b0d10f6716b40c80b4d (patch) | |
tree | 5f2d2f7375796d0b37764a3b924aff0fc4a869ad /src | |
parent | c935b06fab9dc1024ea87908b266a07db73fa595 (diff) | |
download | mongo-4b4f53f64a5b364b74c92b0d10f6716b40c80b4d.tar.gz |
SERVER-34441 error when aborting or commiting a nonexistent txn and starting a txn with one running
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/shell/session.js | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/mongo/shell/session.js b/src/mongo/shell/session.js index 08bce40b94d..bf414d160a4 100644 --- a/src/mongo/shell/session.js +++ b/src/mongo/shell/session.js @@ -700,6 +700,10 @@ var { }; this.startTransaction = function startTransaction(txnOptsObj) { + // If the session is already in a transaction, raise an error. + if (this.isInActiveTransaction()) { + throw new Error("Transaction already in progress on this session."); + } _txnOptions = new TransactionOptions(txnOptsObj); _txnState = ServerSession.TransactionStates.kActive; _nextStatementId = 0; @@ -707,11 +711,19 @@ var { }; this.commitTransaction = function commitTransaction(driverSession) { + // If the session has no active transaction, raise an error. + if (!this.isInActiveTransaction()) { + throw new Error("There is no active transaction to commit on this session."); + } // run commitTxn command return endTransaction("commitTransaction", driverSession); }; this.abortTransaction = function abortTransaction(driverSession) { + // If the session has no active transaction, raise an error. + if (!this.isInActiveTransaction()) { + throw new Error("There is no active transaction to abort on this session."); + } // run abortTxn command return endTransaction("abortTransaction", driverSession); }; @@ -726,13 +738,25 @@ var { } let cmd = {[commandName]: 1, txnNumber: NumberLong(_txnNumber)}; - // writeConcern should only be specified on commit or abort + // writeConcern should only be specified on commit or abort. If a writeConcern is + // not specified from the default transaction options, it will be inherited from + // the session. + if (this.client.getWriteConcern(driverSession) !== undefined) { + cmd.writeConcern = this.client.getWriteConcern(driverSession); + } if (_txnOptions.getTxnWriteConcern() !== undefined) { cmd.writeConcern = _txnOptions.getTxnWriteConcern(); } - // run command against the admin database. - const res = this.client.runCommand(driverSession, "admin", cmd, 0); - _txnState = ServerSession.TransactionStates.kInactive; + + // If commit or abort raises an error, the transaction's state should still change + // to inactive. + let res; + try { + // run command against the admin database. + res = this.client.runCommand(driverSession, "admin", cmd, 0); + } finally { + _txnState = ServerSession.TransactionStates.kInactive; + } return res; }; } @@ -856,7 +880,6 @@ var { // Intentionally ignore command result. this._serverSession.abortTransaction(this); }; - this.commitTransaction_forTesting = function commitTransaction_forTesting() { return this._serverSession.commitTransaction(this); }; |