summaryrefslogtreecommitdiff
path: root/src/mongo/shell
diff options
context:
space:
mode:
authorA. Jesse Jiryu Davis <jesse@mongodb.com>2018-12-20 09:40:47 -0600
committerA. Jesse Jiryu Davis <jesse@mongodb.com>2019-01-03 12:39:58 -0500
commit0507035c67a4399482cf562efbd328ec128f06c6 (patch)
tree2418a00fba3c5a47af28de20058a2a765b600ce5 /src/mongo/shell
parent3a90146b8e7a589e902a6cd0bd3520e971e722ce (diff)
downloadmongo-0507035c67a4399482cf562efbd328ec128f06c6.tar.gz
SERVER-36902 Abort transaction on shell exit, try 2
Diffstat (limited to 'src/mongo/shell')
-rw-r--r--src/mongo/shell/session.js74
1 files changed, 26 insertions, 48 deletions
diff --git a/src/mongo/shell/session.js b/src/mongo/shell/session.js
index 9c68c35a063..d1587440545 100644
--- a/src/mongo/shell/session.js
+++ b/src/mongo/shell/session.js
@@ -486,17 +486,10 @@ var {
// The server session maintains the state of a transaction, a monotonically increasing txn
// number, and a transaction's read/write concerns.
function ServerSession(client) {
- // The default txnState is `inactive` until we call startTransaction.
- let _txnState = ServerSession.TransactionStates.kInactive;
-
let _txnOptions;
// Keep track of the next available statement id of a transaction.
let _nextStatementId = 0;
-
- // _txnNumber starts at -1 because when we increment it, the first transaction
- // and retryable write will both have a txnNumber of 0.
- let _txnNumber = -1;
let _lastUsed = new Date();
this.client = new SessionAwareClient(client);
@@ -511,8 +504,11 @@ var {
wireVersion <= client.getMaxWireVersion();
}
+ const hasTxnState = ((name) => this.handle.getTxnState() === name);
+ const setTxnState = ((name) => this.handle.setTxnState(name));
+
this.isTxnActive = function isTxnActive() {
- return _txnState === ServerSession.TransactionStates.kActive;
+ return hasTxnState("active");
};
this.isFirstStatement = function isFirstStatement() {
@@ -524,11 +520,11 @@ var {
};
this.getTxnNumber = function getTxnNumber() {
- return _txnNumber;
+ return this.handle.getTxnNumber();
};
this.setTxnNumber_forTesting = function setTxnNumber_forTesting(newTxnNumber) {
- _txnNumber = newTxnNumber;
+ this.handle.setTxnNumber(newTxnNumber);
};
this.getTxnOptions = function getTxnOptions() {
@@ -577,11 +573,8 @@ var {
}
if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) {
- // Since there's no native support for adding NumberLong instances and getting back
- // another NumberLong instance, converting from a 64-bit floating-point value to a
- // 64-bit integer value will overflow at 2**53.
- _txnNumber++;
- cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber);
+ this.handle.incrementTxnNumber();
+ cmdObjUnwrapped.txnNumber = this.handle.getTxnNumber();
}
return cmdObj;
@@ -675,22 +668,21 @@ var {
this.assignTxnInfo = function assignTxnInfo(cmdObj) {
// We will want to reset the transaction state to 'inactive' if a normal operation
// follows a committed or aborted transaction.
- if ((_txnState === ServerSession.TransactionStates.kAborted) ||
- (_txnState === ServerSession.TransactionStates.kCommitted &&
- Object.keys(cmdObj)[0] !== "commitTransaction")) {
- _txnState = ServerSession.TransactionStates.kInactive;
+ if ((hasTxnState("aborted")) ||
+ (hasTxnState("committed") && Object.keys(cmdObj)[0] !== "commitTransaction")) {
+ setTxnState("inactive");
}
// If we're not in an active transaction or performing a retry on commitTransaction,
// return early.
- if (_txnState === ServerSession.TransactionStates.kInactive) {
+ if (hasTxnState("inactive")) {
return cmdObj;
}
// If we reconnect to a 3.6 server in the middle of a transaction, we
// catch it here.
if (!serverSupports(kWireVersionSupportingMultiDocumentTransactions)) {
- _txnState = ServerSession.TransactionStates.kInactive;
+ setTxnState("inactive");
throw new Error(
"Transactions are only supported on server versions 4.0 and greater.");
}
@@ -707,10 +699,7 @@ var {
}
if (!cmdObjUnwrapped.hasOwnProperty("txnNumber")) {
- // Since there's no native support for adding NumberLong instances and getting back
- // another NumberLong instance, converting from a 64-bit floating-point value to a
- // 64-bit integer value will overflow at 2**53.
- cmdObjUnwrapped.txnNumber = new NumberLong(_txnNumber);
+ cmdObjUnwrapped.txnNumber = this.handle.getTxnNumber();
}
// All operations of a multi-statement transaction must specify autocommit=false.
@@ -760,18 +749,18 @@ var {
"Transactions are only supported on server versions 4.0 and greater.");
}
_txnOptions = new TransactionOptions(txnOptsObj);
- _txnState = ServerSession.TransactionStates.kActive;
+ setTxnState("active");
_nextStatementId = 0;
- _txnNumber++;
+ this.handle.incrementTxnNumber();
};
this.commitTransaction = function commitTransaction(driverSession) {
// If the transaction state is already 'aborted' we cannot try to commit it.
- if (_txnState === ServerSession.TransactionStates.kAborted) {
+ if (hasTxnState("aborted")) {
throw new Error("Cannot call commitTransaction after calling abortTransaction.");
}
// If the session has no active transaction, raise an error.
- if (_txnState === ServerSession.TransactionStates.kInactive) {
+ if (hasTxnState("inactive")) {
throw new Error("There is no active transaction to commit on this session.");
}
// run commitTxn command
@@ -780,15 +769,15 @@ var {
this.abortTransaction = function abortTransaction(driverSession) {
// If the transaction state is already 'aborted' we cannot try to abort it again.
- if (_txnState === ServerSession.TransactionStates.kAborted) {
+ if (hasTxnState("aborted")) {
throw new Error("Cannot call abortTransaction twice.");
}
// We cannot attempt to abort a transaction that has already been committed.
- if (_txnState === ServerSession.TransactionStates.kCommitted) {
+ if (hasTxnState("committed")) {
throw new Error("Cannot call abortTransaction after calling commitTransaction.");
}
// If the session has no active transaction, raise an error.
- if (_txnState === ServerSession.TransactionStates.kInactive) {
+ if (hasTxnState("inactive")) {
throw new Error("There is no active transaction to abort on this session.");
}
// run abortTxn command
@@ -801,14 +790,14 @@ var {
// transaction as 'committed' or 'aborted' accordingly.
if (this.isFirstStatement()) {
if (commandName === "commitTransaction") {
- _txnState = ServerSession.TransactionStates.kCommitted;
+ setTxnState("committed");
} else {
- _txnState = ServerSession.TransactionStates.kAborted;
+ setTxnState("aborted");
}
return {"ok": 1};
}
- let cmd = {[commandName]: 1, txnNumber: NumberLong(_txnNumber)};
+ let cmd = {[commandName]: 1, txnNumber: this.handle.getTxnNumber()};
// 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.
@@ -826,26 +815,15 @@ var {
res = this.client.runCommand(driverSession, "admin", cmd, 0);
} finally {
if (commandName === "commitTransaction") {
- _txnState = ServerSession.TransactionStates.kCommitted;
+ setTxnState("committed");
} else {
- _txnState = ServerSession.TransactionStates.kAborted;
+ setTxnState("aborted");
}
}
return res;
};
}
- // TransactionStates represents the state of the current transaction. The default state
- // is 'inactive' until startTransaction is called and changes the state to 'active'.
- // Calling abortTransaction or commitTransaction will change the state to 'aborted' or
- // 'committed' respectively, even on error.
- ServerSession.TransactionStates = {
- kActive: 'active',
- kInactive: 'inactive',
- kCommitted: 'committed',
- kAborted: 'aborted',
- };
-
function makeDriverSessionConstructor(implMethods, defaultOptions = {}) {
var driverSessionConstructor = function(client, options = defaultOptions) {
let _options = options;