summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavi Vetriselvan <pvselvan@umich.edu>2018-05-18 07:59:54 -0400
committerPavi Vetriselvan <pvselvan@umich.edu>2018-05-18 08:01:36 -0400
commitb639a8a2b0b037dcf5407683e1698cec3a246fad (patch)
treedef0ead7e782294028c8c779824713d4a4464d72
parent180e9e548df8261a919000aa68b9419b9df093f2 (diff)
downloadmongo-b639a8a2b0b037dcf5407683e1698cec3a246fad.tar.gz
Revert "SERVER-34441 error when aborting or commiting a nonexistent txn and starting a txn with one running"
This reverts commit 1c3d79e1c8eccb4fbc57f7a43e165b3f5b9409f0.
-rw-r--r--jstests/core/txns/multi_statement_transaction_using_api.js20
-rw-r--r--jstests/core/txns/transaction_error_handling.js41
-rw-r--r--src/mongo/shell/session.js33
3 files changed, 25 insertions, 69 deletions
diff --git a/jstests/core/txns/multi_statement_transaction_using_api.js b/jstests/core/txns/multi_statement_transaction_using_api.js
index 1eb213d458f..ce0dfd96bbe 100644
--- a/jstests/core/txns/multi_statement_transaction_using_api.js
+++ b/jstests/core/txns/multi_statement_transaction_using_api.js
@@ -84,6 +84,26 @@
// Verify that we cannot see the document we tried to insert.
assert.eq(null, sessionColl.findOne({_id: "insert-4"}));
+ //
+ // Test that calling startTransaction when a transaction is already running aborts the
+ // current transaction.
+ //
+ jsTestLog("Call startTransaction before committing or aborting the current transaction.");
+ session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});
+
+ assert.commandWorked(sessionColl.insert({_id: "restart-txn-1"}));
+
+ // Try starting a new transaction without committing the previous one. This should result in
+ // the current transaction being implicitly aborted.
+ session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});
+
+ assert.commandWorked(sessionColl.insert({_id: "restart-txn-2"}));
+ session.commitTransaction();
+
+ // Make sure the correct documents exist after committing the second transaction.
+ assert.eq({_id: "restart-txn-2"}, sessionColl.findOne({_id: "restart-txn-2"}));
+ assert.eq(null, sessionColl.findOne({_id: "restart-txn-1"}));
+
jsTestLog("Bulk insert and update operations within transaction.");
session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});
diff --git a/jstests/core/txns/transaction_error_handling.js b/jstests/core/txns/transaction_error_handling.js
deleted file mode 100644
index d30e8c6003e..00000000000
--- a/jstests/core/txns/transaction_error_handling.js
+++ /dev/null
@@ -1,41 +0,0 @@
-// Test basic transaction error handling.
-// @tags: [uses_transactions]
-(function() {
- "use strict";
-
- const dbName = "test";
- const collName = "transaction_error_handling";
- const testDB = db.getSiblingDB(dbName);
-
- testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});
-
- assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}}));
-
- const sessionOptions = {causalConsistency: false};
- const session = testDB.getMongo().startSession(sessionOptions);
- const sessionDb = session.getDatabase(dbName);
- const sessionColl = sessionDb.getCollection(collName);
-
- // Cannot abort or commit a nonexistant transaction.
- try {
- session.commitTransaction();
- } catch (e) {
- assert.eq(e.message, "There is no active transaction to commit on this session.");
- }
-
- try {
- session.abortTransaction();
- } catch (e) {
- assert.eq(e.message, "There is no active transaction to abort on this session.");
- }
-
- // Try to start a transaction with one already running.
- session.startTransaction();
- try {
- session.startTransaction();
- } catch (e) {
- assert.eq(e.message, "Transaction already in progress on this session.");
- }
-
- session.endSession();
-}());
diff --git a/src/mongo/shell/session.js b/src/mongo/shell/session.js
index bf414d160a4..08bce40b94d 100644
--- a/src/mongo/shell/session.js
+++ b/src/mongo/shell/session.js
@@ -700,10 +700,6 @@ 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;
@@ -711,19 +707,11 @@ 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);
};
@@ -738,25 +726,13 @@ var {
}
let cmd = {[commandName]: 1, txnNumber: NumberLong(_txnNumber)};
- // 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);
- }
+ // writeConcern should only be specified on commit or abort
if (_txnOptions.getTxnWriteConcern() !== undefined) {
cmd.writeConcern = _txnOptions.getTxnWriteConcern();
}
-
- // 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;
- }
+ // run command against the admin database.
+ const res = this.client.runCommand(driverSession, "admin", cmd, 0);
+ _txnState = ServerSession.TransactionStates.kInactive;
return res;
};
}
@@ -880,6 +856,7 @@ var {
// Intentionally ignore command result.
this._serverSession.abortTransaction(this);
};
+
this.commitTransaction_forTesting = function commitTransaction_forTesting() {
return this._serverSession.commitTransaction(this);
};