[JAVA] MongoDB Basics: Transaction Processing

Introduction

Transaction support is available since MongoDB 4.0. There are more use cases as transactions can now be processed. image.png

Source: https://docs.mongodb.com/manual/core/transactions-in-applications/

Single node is not supported, is it? If you execute it, an error will occur.

Exception in thread "main" com.mongodb.MongoClientException: Sessions are not supported by the MongoDB cluster to which this client is connected
	at com.mongodb.client.internal.MongoClientImpl.startSession(MongoClientImpl.java:127)
	at com.mongodb.client.internal.MongoClientImpl.startSession(MongoClientImpl.java:113)
	at mongodb.MongoDBTest.main(MongoDBTest.java:24)

Replica Set environment preparation

If you do not have a Replica Set environment, prepare it locally first. image.png Source: https://docs.mongodb.com/manual/replication/

Create a folder for each Node

Node1: /data/mongo-replicaset/node1 Node2: /data/mongo-replicaset/node2 Node3: /data/mongo-replicaset/node3

Start each node


mongod --replSet my-set --dbpath /data/mongo-replicaset/node1 --logpath /data/mongo-replicaset/node1/node1.log --port 27001
mongod --replSet my-set --dbpath /data/mongo-replicaset/node2 --logpath /data/mongo-replicaset/node2/node2.log --port 27002
mongod --replSet my-set --dbpath /data/mongo-replicaset/node3 --logpath /data/mongo-replicaset/node3/node3.log --port 27003

Initialization of Relica Set

Connect to Node1 and initialize.

rs.initiate(
{
        "_id" : "my-set",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "localhost:27001"
                },
                {
                        "_id" : 1,
                        "host" : "localhost:27002"
                },
                {
                        "_id" : 2,
                        "host" : "localhost:27003"
                }
        ]
});

Execution result: image.png

Check the status with rs.status ()

image.png

OK. You are now ready.

Try transaction processing with JAVA

Add multiple records to different DB

MongoDBTest.java


package mongodb;

import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.TransactionOptions;
import com.mongodb.WriteConcern;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.TransactionBody;

import java.util.Date;

import org.bson.Document;

public class MongoDBTest {

	public static void main(String[] args) {
		//Generate MongoDB clients
		MongoClient client = MongoClients.create("mongodb://localhost:27001,localhost:27002,localhost:27003");

		//If the collection does not exist, the transaction will not be processed, so code will create the collection.
        //(Only once. You may add the collection manually)
		client.getDatabase("front").getCollection("access_log").withWriteConcern(WriteConcern.MAJORITY)
				.insertOne(new Document("dummy", 0));
		client.getDatabase("server").getCollection("users").withWriteConcern(WriteConcern.MAJORITY)
				.insertOne(new Document("dummy", 0));

		//Create a session
		ClientSession session = client.startSession();

		//Options definition
		TransactionOptions txnOptions = TransactionOptions.builder().readPreference(ReadPreference.primary())
				.readConcern(ReadConcern.LOCAL).writeConcern(WriteConcern.MAJORITY).build();

		TransactionBody txnBody = new TransactionBody<String>() {
			public String execute() {
				MongoCollection<Document> frontAccessLog = client.getDatabase("front").getCollection("access_log");
				MongoCollection<Document> serverUsers = client.getDatabase("server").getCollection("users");

				//access log
				Document accessLog = new Document();
				accessLog.append("log", "xxxx");
				accessLog.append("acccessDate", new Date());

				frontAccessLog.insertOne(session, accessLog);

				//User data
				Document user = new Document();
				user.append("lastName", "tanaka");
				user.append("firstName", "tarou");
				user.append("createDate", new Date());

				serverUsers.insertOne(session, user);

				return "OK";
			}
		};

		try {
			//Processed in the same transaction
			session.withTransaction(txnBody, txnOptions);

		} catch (RuntimeException e) {
			//Abnormal handling

		} finally {
			session.close();
		}

		//Close the client
		client.close();
	}
}

I was able to insert the data without any problems. image.png

Process multiple records in the same DB

MongoDBTest.java


package mongodb;

import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.TransactionOptions;
import com.mongodb.WriteConcern;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.TransactionBody;
import com.mongodb.client.model.Filters;

import java.util.Date;

import org.bson.Document;

public class MongoDBTest {

	public static void main(String[] args) {
		//Generate MongoDB clients
		MongoClient client = MongoClients.create("mongodb://localhost:27001,localhost:27002,localhost:27003");

		//Create a session
		ClientSession session = client.startSession();

		//Options definition
		TransactionOptions txnOptions = TransactionOptions.builder().readPreference(ReadPreference.primary())
				.readConcern(ReadConcern.LOCAL).writeConcern(WriteConcern.MAJORITY).build();

		TransactionBody txnBody = new TransactionBody<String>() {
			public String execute() {
				MongoCollection<Document> frontAccessLog = client.getDatabase("front").getCollection("access_log");

				//Delete dummy data
				frontAccessLog.deleteOne(Filters.eq("dummy", 0));
				
				//access log
				Document accessLog = new Document();
				accessLog.append("log", "yyyyy");
				accessLog.append("acccessDate", new Date());

				frontAccessLog.insertOne(session, accessLog);
				
				//Make it abnormal
				throw new RuntimeException("Processing failed.");
                // return "OK";
			}
		};

		try {
			//Processed in the same transaction
			session.withTransaction(txnBody, txnOptions);

		} catch (RuntimeException e) {
			//Abnormal handling
			e.printStackTrace();
		} finally {
			session.close();
		}

		//Close the client
		client.close();
	}
}

Since it was thrown abnormally, you can confirm that the DB data has not changed. When I deleted the throw new RuntimeException ("processing failed. ");, I was able to delete and insert the data as expected.

Transaction URL: https://docs.mongodb.com/manual/core/transactions/ Replication: https://docs.mongodb.com/manual/replication/ Construction of RelicaSet: https://docs.mongodb.com/manual/tutorial/deploy-replica-set/ ReplicaSet authentication settings: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-with-keyfile-access-control/

that's all

Recommended Posts

MongoDB Basics: Transaction Processing
Pandas basics for beginners ⑧ Digit processing
Basics of touching MongoDB with MongoEngine
Basics of binarized image processing with Python
[Python3 / MongoDB] Lightly summarize pymongo processing calls