Transactions

Create collection in Mongo Shell

mongo "mongodb+srv://admatic-cluster-7qyyr.mongodb.net/test" --username admatic
MongoDB shell version v4.0.6
Enter password:
Implicit session: session { "id" : UUID("aa7d81e4-1c1d-431c-ab4f-0fdd269c478a") }
MongoDB server version: 4.0.6
MongoDB Enterprise Admatic-Cluster-shard-0:PRIMARY>
use hr
db.createCollection("employees")
db.createCollection("events")

Create Maven Project with MongoDB dependency

Follow the steps in https://pacific-lake-73964.herokuapp.com/Java/

Code

vim src/main/java/com/admatic/Transactions.java
package com.admatic;

import com.mongodb.MongoException;
import com.mongodb.client.*;
import org.bson.Document;

import java.util.logging.Level;
import java.util.logging.Logger;

import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Updates.set;

public final class Transactions {

    private static MongoClient client;

    public static void main(final String[] args) {
        Logger mongoLogger = Logger.getLogger("org.mongodb.driver");
        mongoLogger.setLevel(Level.SEVERE);

        if (args.length == 0) {
            // connect to the local database server
            client = MongoClients.create();
        } else {
            client = MongoClients.create(args[0]);
        }

        Transactions transactions = new Transactions();
        transactions.updateEmployeeInfoWithRetry();
    }

    void runTransactionWithRetry(Runnable transactional) {
        while (true) {
            try {
                transactional.run();
                break;
            } catch (MongoException e) {
                System.out.println("Transaction aborted. Caught exception during transaction.");

                if (e.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)) {
                    System.out.println("TransientTransactionError, aborting transaction and retrying ...");
                    continue;
                } else {
                    throw e;
                }
            }
        }
    }

    void commitWithRetry(ClientSession clientSession) {
        while (true) {
            try {
                clientSession.commitTransaction();
                System.out.println("Transaction committed");

                client.close();
                break;
            } catch (MongoException e) {
                // can retry commit
                if (e.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
                    System.out.println("UnknownTransactionCommitResult, retrying commit operation ...");
                    continue;
                } else {
                    System.out.println("Exception during commit ...");
                    throw e;
                }
            }
        }
    }

    void updateEmployeeInfo() {
        MongoCollection<Document> employeesCollection = client.getDatabase("hr").getCollection("employees");
        MongoCollection<Document> eventsCollection = client.getDatabase("hr").getCollection("events");

        employeesCollection.insertOne(new Document("employee", 3).append("status", "Active"));

        try (ClientSession clientSession = client.startSession()) {
            clientSession.startTransaction();

            employeesCollection.updateOne(clientSession,
                    eq("employee", 3),
                    set("status", "Inactive"));
            eventsCollection.insertOne(clientSession,
                    new Document("employee", 3).append("status", new Document("new", "Inactive").append("old", "Active")));

            commitWithRetry(clientSession);
        }
    }

    void updateEmployeeInfoWithRetry() {
        runTransactionWithRetry(this::updateEmployeeInfo);
    }
}

Compile

mvn compile

Run

mvn exec:java -Dexec.mainClass=com.admatic.Transactions \
    -Dexec.args="mongodb+srv://admatic:admatic123@admatic-cluster-7qyyr.mongodb.net/test"
Transaction committed

results matching ""

    No results matching ""