Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit 17b3f65

Browse files
committed
refactor(txns): move auto start logic to topology
1 parent 19a7a6d commit 17b3f65

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/topologies/replset.js

+13
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,13 @@ ReplSet.prototype.getServers = function() {
11791179
return this.s.replicaSetState.allServers();
11801180
};
11811181

1182+
function ensureTransactionAutostart(session) {
1183+
if (!session) return;
1184+
if (!session.inTransaction() && session.autoStartTransaction) {
1185+
session.startTransaction();
1186+
}
1187+
}
1188+
11821189
//
11831190
// Execute write operation
11841191
function executeWriteOperation(args, options, callback) {
@@ -1240,6 +1247,9 @@ function executeWriteOperation(args, options, callback) {
12401247
incrementTransactionNumber(options.session);
12411248
}
12421249

1250+
// optionally autostart transaction if requested
1251+
ensureTransactionAutostart(options.session);
1252+
12431253
self.s.replicaSetState.primary[op](ns, ops, options, handler);
12441254

12451255
// We need to increment the statement id if we're in a transaction
@@ -1325,6 +1335,9 @@ ReplSet.prototype.command = function(ns, cmd, options, callback) {
13251335
// Establish readPreference
13261336
var readPreference = options.readPreference ? options.readPreference : ReadPreference.primary;
13271337

1338+
// optionally autostart transaction if requested
1339+
ensureTransactionAutostart(options.session);
1340+
13281341
if (
13291342
options.session &&
13301343
options.session.inTransaction() &&

0 commit comments

Comments
 (0)