File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,10 @@ const isPromiseLike = require('./utils').isPromiseLike;
15
15
const ReadPreference = require ( './topologies/read_preference' ) ;
16
16
const isTransactionCommand = require ( './transactions' ) . isTransactionCommand ;
17
17
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 ;
18
22
19
23
function assertAlive ( session , callback ) {
20
24
if ( session . serverSession == null ) {
@@ -185,6 +189,14 @@ class ClientSession extends EventEmitter {
185
189
throw new MongoError ( 'Transaction already in progress' ) ;
186
190
}
187
191
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
+
188
200
// increment txnNumber
189
201
this . incrementTransactionNumber ( ) ;
190
202
Original file line number Diff line number Diff line change @@ -205,6 +205,32 @@ describe('Transactions', function() {
205
205
}
206
206
} ) ;
207
207
} ) ;
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
+ } ) ;
208
234
} ) ;
209
235
210
236
function parseTopologies ( topologies ) {
You can’t perform that action at this time.
0 commit comments