summaryrefslogtreecommitdiff
path: root/src/mongo/db/session.cpp
diff options
context:
space:
mode:
authorWilliam Schultz <william.schultz@mongodb.com>2018-04-14 08:17:36 -0400
committerWilliam Schultz <william.schultz@mongodb.com>2018-04-14 08:17:36 -0400
commit6eafc0790bb3602551127fa831ea859a989c384f (patch)
tree8e715f633eb2dfca26b56c592fa3665104e21512 /src/mongo/db/session.cpp
parent4095f8a2effeedbd17de4792af839b20e1a8e8d5 (diff)
downloadmongo-6eafc0790bb3602551127fa831ea859a989c384f.tar.gz
SERVER-33580 Restrict multi-statement transactions to replica set primaries
Diffstat (limited to 'src/mongo/db/session.cpp')
-rw-r--r--src/mongo/db/session.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/mongo/db/session.cpp b/src/mongo/db/session.cpp
index 9f9d13b6a54..a7747ccf996 100644
--- a/src/mongo/db/session.cpp
+++ b/src/mongo/db/session.cpp
@@ -34,6 +34,7 @@
#include "mongo/db/catalog/index_catalog.h"
#include "mongo/db/commands/feature_compatibility_version_documentation.h"
+#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/concurrency/lock_state.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/db_raii.h"
@@ -316,6 +317,16 @@ void Session::beginOrContinueTxn(OperationContext* opCtx,
return;
}
+ // If the command specified a read preference that allows it to run on a secondary, and it is
+ // trying to execute an operation on a multi-statement transaction, then we throw an error.
+ // Transactions are only allowed to be run on a primary.
+ if (!getTestCommandsEnabled()) {
+ uassert(50789,
+ "readPreference=primary is the only allowed readPreference for multi-statement "
+ "transactions.",
+ !(autocommit && ReadPreferenceSetting::get(opCtx).canRunOnSecondary()));
+ }
+
invariant(!opCtx->lockState()->isLocked());
stdx::lock_guard<stdx::mutex> lg(_mutex);