@@ -166,6 +166,11 @@ Clients require MongoDB drivers updated for MongoDB 4.0.
166
166
To associate read and write operations with a transaction, you **must**
167
167
pass the session to each operation in the transaction.
168
168
169
+ The following example assumes that the collections exists.
170
+
171
+ .. include:: /includes/driver-examples/driver-example-transactions-intro-1.rst
172
+
173
+ Note that the session is passed to each operation in the transaction.
169
174
170
175
Transactions and the ``mongo`` Shell
171
176
------------------------------------
@@ -206,28 +211,7 @@ an element and the transaction as a whole can be retried.
206
211
For example, the following helper runs a function and retries the
207
212
function if a ``"TransientTransactionError"`` is encountered:
208
213
209
- .. code-block:: javascript
210
-
211
- // Runs the txnFunc and retries if TransientTransactionError encountered
212
-
213
- function runTransactionWithRetry(txnFunc, session) {
214
- while (true) {
215
- try {
216
- txnFunc(session); // performs transaction
217
- break;
218
- } catch (error) {
219
- print("Transaction aborted. Caught exception during transaction.");
220
-
221
- // If transient error, retry the whole transaction
222
- if ( error.hasOwnProperty("errorLabels") && error.errorLabels.includes( "TransientTransactionError") ) {
223
- print("TransientTransactionError, retrying transaction ...");
224
- continue;
225
- } else {
226
- throw error;
227
- }
228
- }
229
- }
230
- }
214
+ .. include:: /includes/driver-examples/driver-example-transactions-retry-1.rst
231
215
232
216
Retry Commit Operation
233
217
~~~~~~~~~~~~~~~~~~~~~~
@@ -250,107 +234,15 @@ drivers, applications should take measures to handle
250
234
For example, the following helper commits a transaction and retries if
251
235
a ``"UnknownTransactionCommitResult"`` is encountered:
252
236
253
- .. code-block:: javascript
254
-
255
- // Retries commit if UnknownTransactionCommitResult encountered
256
-
257
- function commitWithRetry(session) {
258
- while (true) {
259
- try {
260
- session.commitTransaction(); // Uses write concern set at transaction start.
261
- print("Transaction committed.");
262
- break;
263
- } catch (error) {
264
- // Can retry commit
265
- if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes( "UnknownTransactionCommitResult") ) {
266
- print("UnknownTransactionCommitResult, retrying commit operation ...");
267
- continue;
268
- } else {
269
- print("Error during commit ...");
270
- throw error;
271
- }
272
- }
273
- }
274
- }
237
+ .. include:: /includes/driver-examples/driver-example-transactions-retry-2.rst
275
238
276
239
Retry Transaction and Commit Operation
277
240
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278
241
279
242
Incorporating logic to retrying the transaction for transient errors
280
243
and retrying the commit, the full code example is:
281
244
282
- .. code-block:: javascript
283
-
284
- // Runs the txnFunc and retries if TransientTransactionError encountered
285
-
286
- function runTransactionWithRetry(txnFunc, session) {
287
- while (true) {
288
- try {
289
- txnFunc(session); // performs transaction
290
- break;
291
- } catch (error) {
292
- // If transient error, retry the whole transaction
293
- if ( error.hasOwnProperty("errorLabels") && error.errorLabels.includes("TransientTransactionError") ) {
294
- print("TransientTransactionError, retrying transaction ...");
295
- continue;
296
- } else {
297
- throw error;
298
- }
299
- }
300
- }
301
- }
302
-
303
- // Retries commit if UnknownTransactionCommitResult encountered
304
-
305
- function commitWithRetry(session) {
306
- while (true) {
307
- try {
308
- session.commitTransaction(); // Uses write concern set at transaction start.
309
- print("Transaction committed.");
310
- break;
311
- } catch (error) {
312
- // Can retry commit
313
- if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes("UnknownTransactionCommitResult") ) {
314
- print("UnknownTransactionCommitResult, retrying commit operation ...");
315
- continue;
316
- } else {
317
- print("Error during commit ...");
318
- throw error;
319
- }
320
- }
321
- }
322
- }
323
-
324
- // Updates two collections in a transactions
325
-
326
- function updateEmployeeInfo(session) {
327
- employeesCollection = session.getDatabase("hr").employees;
328
- eventsCollection = session.getDatabase("reporting").events;
329
-
330
- session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
331
-
332
- try{
333
- employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
334
- eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
335
- } catch (error) {
336
- print("Caught exception during transaction, aborting.");
337
- session.abortTransaction();
338
- throw error;
339
- }
340
-
341
- commitWithRetry(session);
342
- }
343
-
344
- // Start a session.
345
- session = db.getMongo("myRepl/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017").startSession( { mode: "primary" } );
346
-
347
- try{
348
- runTransactionWithRetry(updateEmployeeInfo, session);
349
- } catch (error) {
350
- // Do something with error
351
- } finally {
352
- session.endSession();
353
- }
245
+ .. include:: /includes/driver-examples/driver-example-transactions-retry-3.rst
354
246
355
247
Atomicity
356
248
---------
0 commit comments