【转载】Mongodb锁机制

如果查看在Mongod实例中的lock情况,可以使用如下方式:
1. db.serverStatus
2. db.currentOp
3. mongotop
4. mongostat
5. locks集合

 

MongoDB 在多线程高并发下的问题

https://blog.csdn.net/chenjunxi216/article/details/84628974?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

com.mongodb.DB 这个类有三个很重要的方法 :

 

public abstract void requestStart() :
starts a new "consistent request". Following this call and until requestDone() is called, all db operations should use the same underlying connection. This is useful to ensure that operations happen in a certain order with predictable results.
public abstract void requestDone():
ends the current "consistent request"

public abstract void requestEnsureConnection():
ensure that a connection is assigned to the current "consistent request" (from primary pool, if connected to a replica set) 

具体再查了 MongoDB相关的文档才发现, mongodb 的java driver是不支持是事务性的,所以如果想保证在多线程高并发的情境下不出错(指定的一批数据在同一个底层的数据连接中完成),就必须在使用DB的时候加上上述三个方法。

按照 API 提供的方法说明,在项目代码获得DB的后面加上db.requestStart(),db.requestEnsureConnection(),然后在执行完数据库操作之后加上db.requestDone(),成功解决了数据重复的问题!

 

posted on   白马酒凉  阅读(777)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示