【发布时间】:2016-02-19 05:43:14
【问题描述】:
我有一个场景,我有一个运行的总用户存款。 我正在尝试实现并发机制,以确保不会发生两个并发操作。
我本可以使用乐观并发,但在我的情况下它似乎无法胜任。
在我的情况下,新的存款交易将取决于之前的交易,因此我将在数据库中进行一次读取和一次写入。
据了解,我应该做这样的事情
public DepositTransaction DepositAdd(int userId, decimal ammount)
{
using (var cx = this.Database.GetDbContext())
{
using (var trx = cx.Database.BeginTransaction(System.Data.IsolationLevel.RepeatableRead))
{
try
{
//here the last deposit ammount is read and new created with same context
var transaction = this.DepositTransaction(userId, ammount, SharedLib.BalanceTransactionType.Deposit, cx);
trx.Commit();
return transaction;
}
catch
{
trx.Rollback();
throw;
}
}
}
}
我生成了多个调用该函数的线程,但似乎它们无法获取上一次调用提交的最后数据,也没有函数阻塞并等待上一个线程完成。
【问题讨论】:
标签: mysql sql-server entity-framework