Transaction support is available since MongoDB 4.0. There are more use cases as transactions can now be processed.
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)
If you do not have a Replica Set environment, prepare it locally first. Source: https://docs.mongodb.com/manual/replication/
Node1: /data/mongo-replicaset/node1 Node2: /data/mongo-replicaset/node2 Node3: /data/mongo-replicaset/node3
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
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:
OK. You are now ready.
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.
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