我有一个处理关键事务的操作,但我不确定处理该事务的最佳方式是什么。
这是我需要做的一个简化示例:
[HttpPost]
public ActionResult BeginOrderProcess(Guid orderKey)
{
// Not sure what isolation level I sould use here to start with...
IsolationLevel isolationLevel = IsolationLevel.ReadCommitted;
using(new TransactionScope(isolationLevel)){
// Retreive the order
var order = GetExistingOrder(orderKey);
// Validate that the order can be processed
var validationResult = ValidateOrder(order);
if (!validationResult.Successful)
{
// Order cannot be processed, returning
return View("ErrorOpeningOrder");
}
// Important stuff going on here, but I must be sure it
// will never be called twice for the same order
BeginOrderProcess(order);
return View("OrderedProcessedSuccessfully");
}
}
我要问的第一件事是:在这种操作中,我们可以同时对同一订单有多个请求(即:浏览器对同一订单的快速请求),我是否应该使用悲观锁定来真正确保一个交易时间或有一种方法可以确保BeginOrderProcess
几乎不会同时使用乐观锁定(考虑到它可能会更快)对同一订单的两个并发请求进行两次调用?
第二件事:我这样做是完全错误的方式吗?有更好的方法来处理这样的案例吗?换句话说,我应该如何处理这个?:)