Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Drivers API

On this page

  • Callback API vs Core API
  • Callback API
  • Core API
  • Driver Versions
  • Transaction Error Handling
  • Additional Information

The Callback API:

The Core API:

The callback API incorporates logic:


Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.


The core transaction API does not incorporate retry logic for errors labeled:


Use the Select your language drop-down menu in the upper-right to set the language of the examples on this page.


The following example incorporates logic to retry the transaction for transient errors and retry the commit for unknown commit error:

For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), clients must use MongoDB drivers updated for MongoDB 4.2:

For transactions on MongoDB 4.0 replica sets, clients require MongoDB drivers updated for MongoDB 4.0 or later.

  • Java 3.8.0

  • Python 3.7.0

  • C 1.11.0

  • C# 2.7

  • Node 3.1.0

  • Ruby 2.6.0

  • Perl 2.0.0

  • PHP (PHPC) 1.5.0

  • Scala 2.4.0

Regardless of the database system, whether MongoDB or relational databases, applications should take measures to handle errors during transaction commits and incorporate retry logic for transactions.

The individual write operations inside the transaction are not retryable, regardless of the value of retryWrites. If an operation encounters an error associated with the label "TransientTransactionError", such as when the primary steps down, the transaction as a whole can be retried.

  • The callback API incorporates retry logic for "TransientTransactionError".

  • The core transaction API does not incorporate retry logic for "TransientTransactionError". To handle "TransientTransactionError", applications should explicitly incorporate retry logic for the error.

The commit operations are retryable write operations. If the commit operation encounters an error, MongoDB drivers retry the commit regardless of the value of retryWrites.

If the commit operation encounters an error labeled "UnknownTransactionCommitResult", the commit can be retried.

  • The callback API incorporates retry logic for "UnknownTransactionCommitResult".

  • The core transaction API does not incorporate retry logic for "UnknownTransactionCommitResult". To handle "UnknownTransactionCommitResult", applications should explicitly incorporate retry logic for the error.

On sharded clusters with multiple mongos instances, performing transactions with drivers updated for MongoDB 4.0 (instead of MongoDB 4.2) will fail and can result in errors, including:

Note

Your driver may return a different error. Refer to your driver's documentation for details.

Error Code
Error Message
251
cannot continue txnId -1 for session ... with txnId 1
50940
cannot commit with no participants

For transactions on MongoDB 4.2 deployments (replica sets and sharded clusters), use the MongoDB drivers updated for MongoDB 4.2

The following mongosh methods are available for transactions:

Note

The mongosh example omits retry logic and robust error handling for simplicity's sake. For a more practical example of incorporating transactions in applications, see Transaction Error Handling instead.

// Create collections:
db.getSiblingDB("mydb1").foo.insertOne(
{abc: 0},
{ writeConcern: { w: "majority", wtimeout: 2000 } }
)
db.getSiblingDB("mydb2").bar.insertOne(
{xyz: 0},
{ writeConcern: { w: "majority", wtimeout: 2000 } }
)
// Start a session.
session = db.getMongo().startSession( { readPreference: { mode: "primary" } } );
coll1 = session.getDatabase("mydb1").foo;
coll2 = session.getDatabase("mydb2").bar;
// Start a transaction
session.startTransaction( { readConcern: { level: "local" }, writeConcern: { w: "majority" } } );
// Operations inside the transaction
try {
coll1.insertOne( { abc: 1 } );
coll2.insertOne( { xyz: 999 } );
} catch (error) {
// Abort transaction on error
session.abortTransaction();
throw error;
}
// Commit the transaction using write concern set at transaction start
session.commitTransaction();
session.endSession();
←  TransactionsProduction Considerations →