I'm developing financial application with ASP.NET MVC and Entity Framework(version 5).
In my application, users can approve or reject transaction. If a user make approve, money will be transfer and approve notification email will be sent, otherwise reject notification email will be sent.
What i'm concern is, if two users come to make approval to the same transaction simultaneously. User will get transaction status "WaitForApprove" and do double transfer(in case that both user make approve).
How can i handle this situation, what is the best solution(make sense & low resources consume).
I've do some research, i found Mutex can handle this. But i'm not sure is it the best solution.
Here is my original code look like(Mutex not applied).
public void TransactionApproval(int sysTransferInfoID, string username, string command)
{
// Get the transaction.
var info = _transInfoRepo.GetSingle(i => i.SYS_TRANSFER_INFO_ID == sysTransferInfoID);
if (info.SYS_TRANSFER_STATE_ID != (int)eTransferState.WaitForApprove) // Check status.
{
if (command == "Approve")
{
// Call another service to make transfer here.
// Sent approve fund transfer notification email to other users.
info.SYS_TRANSFER_STATE_ID = (int)eTranferState.Approved; // Change status.
info.APPROVE_BY = username;
}
else if (command == "Reject")
{
// Sent reject fund transfer notification email to other users.
info.SYS_TRANSFER_STATE_ID = (int)eTranferState.Rejected; // Change status.
info.APPROVE_BY = username;
}
_context.SaveChanges();
}
else
{
throw new Exception("Cannot approve transaction.");
}
}