【转载】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(),成功解决了数据重复的问题!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 让容器管理更轻松!