Explain about Atomicity and Transactions in MongoDB?

LSI 8 inserted 4 Atomicity and Transactions in MongoDB:

MongoDB is an open-source document database. It provides high availability, high performance, and auto-scaling. MongoDB has recently updated the latest version i.e MongoDB 4.0, to support the multi-document ACID Transaction properties.

Atomicity:  In database systems, Atomicity is one of the ACID(Atomicity, Consistency, isolation, durability) Transaction properties. In the database atomic operation is an inseparable and complex series of database operations such that either all operations will occur or nothing will occur.

The term atomicity is used to guarantee that there will be no partial database update i.e either the entire lot of data update will happen or nothing will happen at all and partial changes will be rolled back.

An example of an atomic transaction is a money transfer from bank account A to account B. It has two operations, withdrawing the money from account A and saving it to account B.

To perform these tasks in an atomic transaction ensures that the database remains in a consistent state,i.e money is neither lost nor created if either of those two operations fail. For more additional info MongoDB Online Training

Limitations:

 The following are some of the MongoDB atomicity limitations. Those are

  •  There is no support for different documents in atomic transactions in MongoDB.
  • MongoDB provides atomicity operations only for a single document. For example, if a document has fifty fields then the update statement will have either fifty fields or nothing. 
  • Automaticity only maintained at the document-level.

Transactions: A transaction is a piece of work performed in a database management system in opposition to a database and treated as a logical and reliable way independent of other transactions. A transaction generally represents any difference in a database. 

Transaction Sessions:

There are 3 new commands using for creating, committing and aborting transactions.

  • session.startTransaction(): This command is used to start a new transaction in the current session.
  • session.commiTtransaction(): This transaction is used for saving consistency and durability changes made by the operations in the transactions.
  • session.AbortTransaction(): This transaction will end without saving any of the changes made by the operations in the transactions. 

Transaction properties:

The MongoDB  transactions will have the following properties:

  •  When the transaction is committed, all the data changes in the transactions are saved.
  • If any operation in the transaction fails, the transaction terminated.
  • When a transaction terminated, all data changes made in the transaction are discarded. Get practical knowledge from MongoDB Online Course
  • Until a transaction is performed, no write operations in the transaction are visible outside the transaction.

ACID Transactions in MongoDB:

The following are ACID transactions in MongoDB:

Atomicity:  It guarantees each transaction treated as a single “unit”, which either succeeds completely or fails completely,

Consistency: Consistency is that any transaction happens on a database will always bring the database from one to another valid state.

Isolation: This property defines how the changes made by one operation become visible to other simultaneous operations.

Durability: In this, once the transaction is committed and it will always remain permanent irrespective of database crashes, power loss, or errors.

Limitations of Transactions:

        To support transactions we have to know some limitations. They are as follows:

  • A quantity must exist in order to use transactions.
  • A quantity can not be created or dropped inside a transaction.  
  • An index can not be created or dropped inside a transaction.
  • Non-CRUD operations are not permitted inside a transaction.
  • A transaction can not read or write in the config, admin, and local databases.  
  • The size of the transaction is limited to 16MB only.    
  • By default, a transaction that executes for longer than 60 seconds will automatically expire.        

MongoDB Transactions for different technologies:

MongoDB Transaction in c#: 

Following code shows about the MongoDb transactions in C#

CSharpTransactions.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Bson.Serialization.Attributes;

namespace MongoDBTransaction
{
    class Program
    {
        public class Product
        {
            [BsonId]
            public ObjectId Id { get; set; }
            [BetonElement("SKU")]
            public int SKU { get; set; }
            [BsonElement("Description")]
            public string Description { get; set; }
            [BsonElement("Price")]
            public Double Price { get; set; }
        }
       
        const string MongoDBConnectionString = "mongodb+srv://<<MONGODB CONNECTION STRING>>";
 
        static async Task Main(string[] args)
        {
           if (!await UpdateProducts()) { Environment.Exit(0); }
            Console.WriteLine("Finished updating the product collection");
            Console.ReadKey();
        }
        static async Task<bool> UpdateProducts()
        {
            //Generate client connection to our MongoDB database
            var client = new MongoClient(MongoDBConnectionString);

            // Generate a session object that is used when leveraging transactions
            var session = client.StartSession();

            //Generate the collection object that represents the "products" collection
            var products = session.Client.GetDatabase("MongoDBStore").GetCollection<Product>("products");
           
            //approve the collection if there is data in there
            products.Database.DropCollection("products");

            //Generate  some sample data
            var TV = new Product { Declaration = "Television", SKU = 4001, Price = 2000 };
            var Book = new Product { Declaration= "A funny book", SKU = 43221, Price = 19.99 };
            var DogBowl = new Product { Declaration= "Bowl to Fido", SKU = 123, Price = 40.00 };

            //Begin transaction
            session.StartTransaction();

            try
            {
                //Insert the sample data 
                await products.InsertOneAsync(TV);
                await products.InsertOneAsync(Book);
                await products.InsertOneAsync(DogBowl);

                var filter = new FilterDefinitionBuilder<Product>().Empty;
                var results = await products.Find<Product>(filter).ToListAsync();
                Console.WriteLine("Original Prices:\n");
                foreach (Product d in results)
                {
                    Console.WriteLine(String.Format("Product Name: {0}\tPrice: {1:0.00}", d.Description, d.Price));
                }

                // develop all the prices by 10% for all products
                var update = new UpdateDefinitionBuilder<Product>().Mul<Double>(r=>r.Price,1.1);
                await products.UpdateManyAsync(filter, update); //,options);

                //Made it here without error? Let's commit the transaction
                session.CommitTransaction();

                //Let's print the new results to the console
                Console.WriteLine("Original Prices:\n");
                results = await products.Find<Product>(filter).ToListAsync();
                foreach (Product d in results)
                {
                    Console.WriteLine(String.Format("Product Name: {0}\tPrice: {1:0.00}", d.Description, d.Price));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error writing to MongoDB: " + e.Message);
                session.AbortTransaction();
            }
            return true;
        }
      }
}

MongoDB Transaction in Java:

 The following code will demonstrate about the MongoDB transaction  in java Online Training

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

            if ((ErrorLabel(MongoException.TRANSITORY_TRANSACTION_ERROR_LABEL)) {
                       continue;
            } else {
                throw e;
            }
        }
    }
}

void commitWithRetry(ClientSession clientSession) {
    while (true) {
        try {
            clientSession.commitTransaction();
            System.out.println("Transaction committed");
            break;
        } catch (MongoException e) {
            // can retry commit
            if ((hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
                System.out.println("UnknownTransactionCommitResult, retrying commit operation ...")
                continue;
            } else {
                System.out.println("Exception during commit ...");
                throw e;
            }
        }

MongoDB transaction in NOdeJS: the following code shows about the Transaction in nodeJS:

const client = new MongoClient(uri);
  await client.connect();

  await client
    .db('mydb1')
    .collection('foo')
    .insertOne({ abc: 0 }, { w: 'majority' });

  await client
    .db('mydb2')
    .collection('bar')
    .insertOne({ xyz: 0 }, { w: 'majority' });

  // Step 1: Start a Client Session
  const session = client.startSession();

  // Step 2: Optional. Define options to use for the transaction
  const transactionOptions = {
    readPreference: 'primary',
    readConcern: { level: 'local' },
    writeConcern: { w: 'majority' }
  };

  // Step 3: Use with the transaction to start a transaction, execute the callback, and commit (or abort on the error)
  // Note: The callback for with transaction MUST be async and/or return a Promise.
  try {
    await session.withTransaction(async () => {
      const coll1 = client.db('mydb1').collection('foo');
      const coll2 = client.db('mydb2').collection('bar');

      // Important:: You must pass the session to the operations

      await coll1.insertOne({ abc: 1 }, { session });
      await coll2.insertOne({ xyz: 999 }, { session });
    }, transactionOptions);
  } finally {
    await session.endSession();
    await client.close();
  }

In this article, I have given information about the Atomicity and transactions in MongoDB. I hope this gives an idea about the atomicity and transactions of MongoDB in different technologies.

To get in-depth knowledge, enroll for a live free demo on MongoDB Training

Leave a comment

Design a site like this with WordPress.com
Get started