Skip to content

Commit 3a1fdc1

Browse files
kvwalkerdaprahamian
authored andcommitted
fix(sessions): ensure an error is thrown when attempting sharded transactions
Fixes NODE-1931
1 parent b95d64e commit 3a1fdc1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/core/sessions.js

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const isPromiseLike = require('./utils').isPromiseLike;
1515
const ReadPreference = require('./topologies/read_preference');
1616
const isTransactionCommand = require('./transactions').isTransactionCommand;
1717
const resolveClusterTime = require('./topologies/shared').resolveClusterTime;
18+
const isSharded = require('./wireprotocol/shared').isSharded;
19+
const maxWireVersion = require('./utils').maxWireVersion;
20+
21+
const MAX_FOR_TRANSACTIONS = 7;
1822

1923
function assertAlive(session, callback) {
2024
if (session.serverSession == null) {
@@ -185,6 +189,14 @@ class ClientSession extends EventEmitter {
185189
throw new MongoError('Transaction already in progress');
186190
}
187191

192+
const topologyMaxWireVersion = maxWireVersion(this.topology);
193+
if (
194+
isSharded(this.topology) ||
195+
(topologyMaxWireVersion != null && topologyMaxWireVersion < MAX_FOR_TRANSACTIONS)
196+
) {
197+
throw new MongoError('Transactions are not supported on sharded clusters in MongoDB < 4.2.');
198+
}
199+
188200
// increment txnNumber
189201
this.incrementTransactionNumber();
190202

test/functional/transactions_tests.js

+26
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,32 @@ describe('Transactions', function() {
205205
}
206206
});
207207
});
208+
209+
describe('startTransaction', function() {
210+
it('should error if transactions are not supported', {
211+
metadata: { requires: { topology: ['sharded'], mongodb: '>4.0.0' } },
212+
test: function(done) {
213+
const configuration = this.configuration;
214+
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
215+
216+
client.connect((err, client) => {
217+
const session = client.startSession();
218+
const db = client.db(configuration.db);
219+
const coll = db.collection('transaction_error_test');
220+
coll.insertOne({ a: 1 }, err => {
221+
expect(err).to.not.exist;
222+
expect(() => session.startTransaction()).to.throw(
223+
'Transactions are not supported on sharded clusters in MongoDB < 4.2.'
224+
);
225+
226+
session.endSession(() => {
227+
client.close(done);
228+
});
229+
});
230+
});
231+
}
232+
});
233+
});
208234
});
209235

210236
function parseTopologies(topologies) {

0 commit comments

Comments
 (0)