summaryrefslogtreecommitdiff
path: root/src/mongo/shell/session.js
diff options
context:
space:
mode:
authorPavi Vetriselvan <pvselvan@umich.edu>2018-05-18 11:33:18 -0400
committerPavi Vetriselvan <pvselvan@umich.edu>2018-05-18 11:46:06 -0400
commit4b4f53f64a5b364b74c92b0d10f6716b40c80b4d (patch)
tree5f2d2f7375796d0b37764a3b924aff0fc4a869ad /src/mongo/shell/session.js
parentc935b06fab9dc1024ea87908b266a07db73fa595 (diff)
downloadmongo-4b4f53f64a5b364b74c92b0d10f6716b40c80b4d.tar.gz
SERVER-34441 error when aborting or commiting a nonexistent txn and starting a txn with one running
Diffstat (limited to 'src/mongo/shell/session.js')
-rw-r--r--src/mongo/shell/session.js33
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);
};