简体   繁体   English

锁语句和MVC

[英]lock statement and MVC

Is it plausable to use a lock for a POST within MVC? 在MVC中为POST使用锁是否合理? Is there situation like using a webfarm that would invalidate the lock? 是否存在使用Webfarm这样会使锁失效的情况? I usually use database locks but my particular situation is pretty difficult to manage and its simpler to use a Monitor. 我通常使用数据库锁,但是我的特殊情况很难管理,使用监视器也更简单。 I dont want to get into discussing why I need a lock just want to know if there are situations when it wont work as desired. 我不想讨论为什么我需要锁,只是想知道在某些情况下它无法按需工作。

private static object Lockable = new Object(); 
public ActionResult Submit() 
{
     lock(Lockable)
     {


     }   
}

In order to achieve a lock over a webfarm you need to have a shared resource on the network (such as a cache server), and you need to use that to achieve webfarm-safe locking. 为了实现对Webfarm的锁定,您需要在网络上拥有一个共享资源(例如缓存服务器),并且需要使用该资源来实现Webfarm安全锁定。 I think that's the simplest solution. 我认为这是最简单的解决方案。

You can also isolate the component you want to serialize calls to and access to it over network. 您也可以隔离要序列化调用并通过网络访问它的组件。

There is another option of creating a MSDTC compliant component that can enlist into distributed transactions so all callers are enforced to serialize their calls. 还有一个创建兼容MSDTC的组件的选项,该组件可以加入分布式事务,因此将强制所有调用方序列化其调用。 However that requires setting up all environment to work with MSDTC, Enterprise Services etc. 但是,这需要设置所有环境才能与MSDTC,企业服务等配合使用。

正如您自己说的那样,在少数Web服务器/不同进程的情况下,您的锁仅对当前进程起作用,这意味着它不会影响您的应用程序的其他正在运行的实例(如果存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM