[DRIVERS-488] Provide Transactions example for Docs Created: 21/May/18  Updated: 28/Oct/23  Resolved: 28/Feb/19

Status: Closed
Project: Drivers
Component/s: Docs Examples
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Kay Kim (Inactive) Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CXX-1583 Provide Transactions example for Docs Closed
depends on GODRIVER-422 Provide Transactions example for Docs Closed
depends on PHPLIB-350 Provide Transactions example for Docs Closed
depends on RUST-813 Provide transactions examples for docs Closed
depends on CDRIVER-2663 Provide Transactions example for Docs Closed
depends on CSHARP-2277 Provide Transactions example for Docs Closed
depends on JAVA-2866 Provide Transactions example for Docs Closed
depends on MOTOR-224 Provide Transactions example for Docs Closed
depends on NODE-1478 Provide Transactions example for Docs Closed
depends on PYTHON-1562 Provide Transactions example for Docs Closed
depends on RUBY-1340 Provide Transactions example for Docs Closed
Gantt Dependency
has to be done before DRIVERS-577 Update Transactions Retry Example 3 t... Closed
Related
related to DOCS-11975 Transaction example for C# does not u... Closed
is related to MOTOR-427 Provide transaction retry examples fo... Closed
Driver Compliance:
Key Status/Resolution FixVersion
NODE-1478 Fixed 3.1.0
SCALA-408 Fixed 2.4.0
PYTHON-1562 Fixed 3.7
CSHARP-2277 Fixed 2.7.0
PERL-908 Fixed 2.0.0
RUBY-1340 Fixed 2.6.0
JAVA-2866 Fixed
CXX-1583 Fixed 3.4.0
PHPLIB-350 Fixed 1.4.0
MOTOR-224 Fixed 2.0
GODRIVER-422 Fixed 1.0.0-rc1
CDRIVER-2663 Fixed 1.11.0, 1.12.0
SWIFT-423 Fixed 1.0.0
RUST-813 Fixed 2.0.0-beta.1

 Description   

Hi –
for the manual – would like to pop in driver examples in
Drivers section and Retry section

If we could get the corresponding driver examples.

  • The Intro example will be for the Drivers section – where it's to highlight – how to use a transaction, and the section will be emphasizing the passing of the session to the operations in the txn.
  • The retry examples will of course be in the retry section.

 
// Start Transactions Intro Example 1
 
function updateEmployeeInfo(session) {
    employeesCollection = session.getDatabase("hr").employees;
    eventsCollection = session.getDatabase("reporting").events;
 
    session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
 
    try{
        employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
        eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
    } catch (error) {
        print("Caught exception during transaction, aborting.");
        session.abortTransaction();
        throw error;
    }
 
    while (true) {
        try {
            session.commitTransaction(); // Uses write concern set at transaction start.
            print("Transaction committed.");
            break;
        } catch (error) {
            // Can retry commit
            if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes("UnknownTransactionCommitResult") ) {
                print("UnknownTransactionCommitResult, retrying commit operation ...");
                continue;
            } else {
                print("Error during commit ...");
                throw error;
            }
       }
    }
}
// End Transactions Intro Example 1
 
 
// Start Transactions Retry Example 1
function runTransactionWithRetry(txnFunc, session) {
    while (true) {
        try {
            txnFunc(session);  // performs transaction
            break;
        } catch (error) {
            print("Transaction aborted. Caught exception during transaction.");
 
            // If transient error, retry the whole transaction
            if ( error.hasOwnProperty("errorLabels") && error.errorLabels.includes( "TransientTransactionError")  ) {
                print("TransientTransactionError, retrying transaction ...");
                continue;
            } else {
                throw error;
            }
        }
    }
}
// End Transactions Retry Example 1
 
// Start Transactions Retry Example 2
 
function commitWithRetry(session) {
    while (true) {
        try {
            session.commitTransaction(); // Uses write concern set at transaction start.
            print("Transaction committed.");
            break;
        } catch (error) {
            // Can retry commit
            if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes( "UnknownTransactionCommitResult") ) {
                print("UnknownTransactionCommitResult, retrying commit operation ...");
                continue;
            } else {
                print("Error during commit ...");
                throw error;
            }
       }
    }
}
// End Transactions Retry Example 2
 
// Start Transactions Retry Example 3
 
function runTransactionWithRetry(txnFunc, session) {
    while (true) {
        try {
            txnFunc(session);  // performs transaction
            break;
        } catch (error) {
            // If transient error, retry the whole transaction
            if ( error.hasOwnProperty("errorLabels") && error.errorLabels.includes("TransientTransactionError")  ) {
                print("TransientTransactionError, retrying transaction ...");
                continue;
            } else {
                throw error;
            }
        }
    }
}
 
function commitWithRetry(session) {
    while (true) {
        try {
            session.commitTransaction(); // Uses write concern set at transaction start.
            print("Transaction committed.");
            break;
        } catch (error) {
            // Can retry commit
            if (error.hasOwnProperty("errorLabels") && error.errorLabels.includes("UnknownTransactionCommitResult") ) {
                print("UnknownTransactionCommitResult, retrying commit operation ...");
                continue;
            } else {
                print("Error during commit ...");
                throw error;
            }
       }
    }
}
 
// Updates two collections in a transactions
 
function updateEmployeeInfo(session) {
    employeesCollection = session.getDatabase("hr").employees;
    eventsCollection = session.getDatabase("reporting").events;
 
    session.startTransaction( { readConcern: { level: "snapshot" }, writeConcern: { w: "majority" } } );
 
    try{
        employeesCollection.updateOne( { employee: 3 }, { $set: { status: "Inactive" } } );
        eventsCollection.insertOne( { employee: 3, status: { new: "Inactive", old: "Active" } } );
    } catch (error) {
        print("Caught exception during transaction, aborting.");
        session.abortTransaction();
        throw error;
    }
 
    commitWithRetry(session);
}
 
// Start a session.
session = db.getMongo("myRepl/mongodb0.example.net:27017,mongodb1.example.net:27017,mongodb2.example.net:27017").startSession( { mode: "primary" } );
 
try{
   runTransactionWithRetry(updateEmployeeInfo, session);
} catch (error) {
   // Do something with error
} finally {
   session.endSession();
}
 
// End Transactions Retry Example 3

cc: rathi.gnanasekaran scott.lhommedieu behackett



 Comments   
Comment by A. Jesse Jiryu Davis [ 12/Sep/18 ]

kay.kim for historical accuracy, would you mind updating this ticket with the actual final example you wanted? I see for example that the final Python example is not like the example in the description of this ticket:

https://github.com/mongodb/mongo-python-driver/blob/e6b0f3847d4135a46880592e033cf30e58ea5b24/test/test_examples.py#L867

Generated at Thu Feb 08 08:21:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.