[GODRIVER-422] Provide Transactions example for Docs Created: 23/May/18  Updated: 28/Oct/23  Resolved: 20/Feb/19

Status: Closed
Project: Go Driver
Component/s: Documentation
Affects Version/s: None
Fix Version/s: 1.0.0-rc1

Type: Task Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Kristofer Brandow (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by DRIVERS-488 Provide Transactions example for Docs Closed
Related

 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

 



 Comments   
Comment by Githook User [ 20/Feb/19 ]

Author:

{'name': 'Kris Brandow', 'username': 'skriptble', 'email': 'kris@mongodb.com'}

Message: Add transactions examples for docs

GODRIVER-422
GODRIVER-828
GODRIVER-788

Change-Id: I5927c58c4ecf89ab8124c87066b394ca6a6b8dc8
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/9ff076f5844dbdfbc027a2a2aed84b8a9726097c

Comment by Kristofer Brandow (Inactive) [ 08/Jan/19 ]

I think the idea is that we could have someone on the docs team add those new examples to the Go files. It's pretty simple and straight forward.

Comment by Ian Whalen (Inactive) [ 08/Jan/19 ]

kris.brandow?

Comment by Susan Kerschbaumer (Inactive) [ 08/Jan/19 ]

ian.whalen scott.lhommedieu is there any way we can do these examples the way Chris has been doing them for the other functionality which is by having him write them as commented tests that we incorporate into our docs? That would probably be the quickest way to get these done.

Comment by Ian Whalen (Inactive) [ 07/Jan/19 ]

michele.knaub It sounds like Scott talked to someone on Docs about getting this onto their plate to do. What's the best way to get this ticket onto your work queue? Assign? Move to different project? something else?

CC scott.lhommedieu

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