java - 使用事务时对 MongoDB 服务器进行了多少次往返?
问题描述
我想知道在使用事务 MongoDB 时对服务器进行了多少次往返?例如,如果像这样使用 Java 驱动程序:
ClientSession clientSession = client.startSession();
TransactionOptions txnOptions = TransactionOptions.builder()
.readPreference(ReadPreference.primary())
.readConcern(ReadConcern.LOCAL)
.writeConcern(WriteConcern.MAJORITY)
.build();
TransactionBody txnBody = new TransactionBody<String>() {
public String execute() {
MongoCollection<Document> coll1 = client.getDatabase("mydb1").getCollection("foo");
MongoCollection<Document> coll2 = client.getDatabase("mydb2").getCollection("bar");
coll1.insertOne(clientSession, new Document("abc", 1));
coll2.insertOne(clientSession, new Document("xyz", 999));
return "Inserted into collections in different databases";
}
};
try {
clientSession.withTransaction(txnBody, txnOptions);
} catch (RuntimeException e) {
// some error handling
} finally {
clientSession.close();
}
在这种情况下,两个文档存储在一个事务中:
coll1.insertOne(clientSession, new Document("abc", 1));
coll2.insertOne(clientSession, new Document("xyz", 999));
“插入操作”是堆叠起来并在一次往返中发送到服务器,还是实际上对服务器进行了两次调用(或更多?)?
解决方案
每个插入单独发送。您可以一起使用批量写入批量写入操作。
最后的提交也是一个单独的操作。
推荐阅读
- python - 为什么在 python 中下载图像时必须同时保存 png 和 txt 文件?
- javascript - 运行 npm run make 时出现“类型错误:无法读取未定义的属性‘日期’”
- android - 我可以将手机用作 VIsual Studio 2019 的模拟器吗?
- java - Android Studio ...在意图中将查询值传递给互联网搜索
- revit-api - Revit 在类型目录中使用什么字符编码?
- graph - 此图的名称/类型
- docker - 通过 Azure 应用服务的 Terraform 脚本将变量传递给 Docker Compose
- types - Julia:是否可以将参数字典传递给函数?
- amazon-web-services - 在 AWS Elastic Beanstalk 上运行烧瓶应用程序时出现 504 网关超时
- javascript - 填充所有输入后如何激活动画?