Handling transactions in Java MongoDB

MongoDB is a popular NoSQL database that provides high scalability and flexibility for storing and retrieving data. Starting from version 4.0, MongoDB also introduced support for multi-document transactions, making it even more suitable for transactional applications. In this blog post, we will explore how to handle transactions in Java MongoDB.

Prerequisites

Before getting started, make sure you have the following prerequisites in place:

  1. Java Development Kit (JDK) installed on your machine.
  2. MongoDB server running locally or remotely with version 4.0 or above.
  3. Maven or Gradle installed for dependency management.

Setting up MongoDB Connection

To connect to MongoDB from a Java application, we need to add the MongoDB Java driver dependency to our project. If you are using Maven, add the following dependency to your pom.xml file:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>4.4.3</version>
</dependency>

If you are using Gradle, add the following dependency to your build.gradle file:

implementation 'org.mongodb:mongodb-driver-sync:4.4.3'

Next, let’s create a MongoDB client and connect to the MongoDB server:

import com.mongodb.*;
import com.mongodb.client.*;

public class MongoDBConnection {
    private static MongoClient mongoClient;
    private static MongoDatabase database;

    public static void main(String[] args) {
        mongoClient = MongoClients.create("mongodb://localhost:27017");
        database = mongoClient.getDatabase("mydb");

        // Perform operations...
    }
}

Replace mongodb://localhost:27017 with your MongoDB server connection URI. Also, you can replace "mydb" with your own database name.

Handling Transactions

To handle transactions in Java MongoDB, we need to make use of the ClientSession class provided by the MongoDB Java driver. Here’s an example of how to perform a transaction:

import com.mongodb.*;
import com.mongodb.client.*;
import org.bson.*;
import com.mongodb.client.model.*;
import com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;

public class MongoDBTransaction {
    public static void main(String[] args) {
        try (ClientSession session = mongoClient.startSession()) {
            session.startTransaction();

            try {
                MongoCollection<Document> collection = database.getCollection("mycollection");

                // Perform operations within the transaction
                collection.insertOne(session, new Document("name", "John Doe"));
                collection.updateOne(session, eq("name", "John Doe"), set("age", 30));
                collection.deleteOne(session, eq("age", 30));

                session.commitTransaction();
            } catch (Exception e) {
                session.abortTransaction();
                throw e;
            }
        }
    }
}

The above code starts a new transaction by calling session.startTransaction(). Within the transaction, we can perform various operations on the MongoCollection using the provided session object. Once all the operations are successfully executed, we call session.commitTransaction() to commit the changes. If any exception occurs during the transaction, we call session.abortTransaction() to roll back the changes.

Conclusion

Handling transactions in Java MongoDB is made possible with the introduction of multi-document transactions in MongoDB 4.0 and above. By using the ClientSession object and the provided methods, we can perform multiple operations in a transaction and ensure atomicity and consistency in our application.