Skip to content

Commit

Permalink
fix(sessions): ensure an error is thrown when attempting sharded tran…
Browse files Browse the repository at this point in the history
…sactions

Fixes NODE-1931
  • Loading branch information
kvwalker authored and daprahamian committed Aug 13, 2019
1 parent b95d64e commit 3a1fdc1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/core/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const isPromiseLike = require('./utils').isPromiseLike;
const ReadPreference = require('./topologies/read_preference');
const isTransactionCommand = require('./transactions').isTransactionCommand;
const resolveClusterTime = require('./topologies/shared').resolveClusterTime;
const isSharded = require('./wireprotocol/shared').isSharded;
const maxWireVersion = require('./utils').maxWireVersion;

const MAX_FOR_TRANSACTIONS = 7;

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

const topologyMaxWireVersion = maxWireVersion(this.topology);
if (
isSharded(this.topology) ||
(topologyMaxWireVersion != null && topologyMaxWireVersion < MAX_FOR_TRANSACTIONS)
) {
throw new MongoError('Transactions are not supported on sharded clusters in MongoDB < 4.2.');
}

// increment txnNumber
this.incrementTransactionNumber();

Expand Down
26 changes: 26 additions & 0 deletions test/functional/transactions_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ describe('Transactions', function() {
}
});
});

describe('startTransaction', function() {
it('should error if transactions are not supported', {
metadata: { requires: { topology: ['sharded'], mongodb: '>4.0.0' } },
test: function(done) {
const configuration = this.configuration;
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });

client.connect((err, client) => {
const session = client.startSession();
const db = client.db(configuration.db);
const coll = db.collection('transaction_error_test');
coll.insertOne({ a: 1 }, err => {
expect(err).to.not.exist;
expect(() => session.startTransaction()).to.throw(
'Transactions are not supported on sharded clusters in MongoDB < 4.2.'
);

session.endSession(() => {
client.close(done);
});
});
});
}
});
});
});

function parseTopologies(topologies) {
Expand Down

0 comments on commit 3a1fdc1

Please sign in to comment.