public boolean mongoInsertManyTransaction(final MongoClient client, List<Document> document) {
TransactionOptions txnOptions = TransactionOptions.builder()
.readPreference(ReadPreference.primary())
.readConcern(ReadConcern.MAJORITY)
.writeConcern(WriteConcern.MAJORITY)
.build();
// 事务逻辑
try (ClientSession clientSession = client.startSession()) {
clientSession.withTransaction(
() -> {
// 获取数据库
MongoDatabase db = client.getDatabase(dbName);
MongoCollection<Document> collection = db.getCollection(collectionName);
//插入多条文档数据
collection.insertMany(clientSession, document);
return true;
},
txnOptions
);
}
return false;
}
Mongodb事务demo
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- mongodb 4.0已经支持副本集级别的事务了,而且现在是稳定版.下一个版本4.2准备支持分片的事物.我因为数据...
- Spring事务代理机制总结: 1、Spring声明式事务的底层是怎么实现的? 通过Spring中的代理,那在Sp...
- 参考了很多网上的教程做了一个简易的多数据源切换demo: 1.pom.xml 依赖准备: 2.yml文件 3.项目...