MongoDB - 集合的悲观锁定 [英] MongoDB - Pessimistic Locking of a Collection

查看:46
本文介绍了MongoDB - 集合的悲观锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常感谢您的时间和创造性的投入,或许可以在我一直试图弄清楚的事情上指导我大方向.

I would greatly appreciate your time and creative input in perhaps guiding me in general direction on something I've been trying to figure out.

我打算允许用户按订单操作集合.它是一个基于股票市场的应用程序,因此我确定以这种方式进行操作至关重要.当用户对股票下某种订单时,我需要它按订单执行订单,单个订单相当于股票集合中的一个文档.在前一个订单完成后,订单队列将可用于更多订单.我尝试实现了一些手动锁定方法,但我仍然遇到了在订单队列中没有下一个订单的订单上执行订单的问题.

I have intentions of allowing users to manipulate a collection on an order-by-order basis. It is a stock market based application, so I have determined that it is crucial to do it in this fashion. When a user places some sort of order on a stock, I need it to execute order-by-order, and a single order is the equivalent of one single document in a stock's collection. The order queue would then be available for more orders after the previous one has been completed. I have attempted to implement some manual locking methods, but I still run into the problem of orders executing on orders that are not next in the order queue.

var stringType = "locked";
var checkIt = await stocksLS.collection(data[5]).findOneAndUpdate({"editor": {$exists:false}},
{$set: {editor: [stringType]}},
{
sort:{"Price":1,"TS":1}
}
);

在这段代码中,data[5] 只是股票的股票代码,然后我锁定了下一个最佳订单(按最低价格和最早时间戳排序)以确保订单仅由一个用户.但是,如果多个用户同时执行一个订单,它显然只是开始执行离下一个最佳订单越来越远的未来订单,这不是预期的(因为它们仍然没有按照此代码锁定).

In this snippet of code, data[5] is just the ticker symbol of a stock, then I throw a lock on the next best order (sorted by lowest price and earliest timestamp) to ensure that order gets executed on solely by one user. However, if multiple users execute an order simultaneously, it obviously just starts executing on future orders that are further and further away from the next best order which is not intended (since they are still not locked per this code).

我最好的解决方案是在订单执行时锁定所有文档,然后让传入的订单尝试重复排队订单队列集合,直到它附加锁.

My best solution would be to just place a lock on all of the documents while an order executes and then have incoming orders try to queue the order queue collection repeatedly until it attaches a lock.

这一切可能有更好的解决方案,但这是我想出的最好的解决方案,因此我非常感谢您对此事的意见,以指导获得更好的想法和解决方案.非常感谢.

There is likely a much better solution to this all, but it is the best I have come up with, so I would appreciate your input on the matter to get guided toward better ideas and solutions. Thank you very much.

推荐答案

如果要使用队列,请使用队列.一个实际的.使用您选择的语言 (JS) 查找队列库.

If you want to use a queue, use a queue. An actual one. Find a queue library in the language of your choice (JS).

您可以尝试实现一个队列,但与其解决所有不可避免的错误,不如使用一个已经存在的、经过调试的错误.

You could attempt to implement a queue, but instead of fighting all of the bugs that will inevitably pop up just use an already existing, debugged one.

MongoDB 不是队列,也没有在服务器中实现队列.

MongoDB is not a queue, nor does it implement a queue in the server.

这篇关于MongoDB - 集合的悲观锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆