【发布时间】:2013-09-12 19:34:59
【问题描述】:
我为每个请求使用一个 TransactionScope,如下所示:
public ActionResult Login(string user, string pass)
{
using (ServerContext context = new ServerContext ())
{
TransactionOptions transOptions = new TransactionOptions();
transOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
transOptions.Timeout = TransactionManager.MaximumTimeout;
using (var scope = new TransactionScope(TransactionScopeOption.Required, transOptions))
{
// Some logic and Linq queries here
}
}
我在事务范围内执行了一些插入、更新、删除和过程调用 但是,我遇到了一些罕见的高负载死锁异常。所以,我做得对吗? 还是最好为每个操作打开一个 TransactionScope? (但如果其中一个集合失败,我需要回滚所有操作。)
谢谢。
【问题讨论】:
-
一般来说,这是正确的。您是否真的在 SQL Server 中死锁,或者您的意思是阻塞导致请求超时?
-
只有当您有多个应该是事务性的
SaveChanges调用时,您才需要一个 TS。我猜你就是这么做的?
标签: c# asp.net-mvc-4 entity-framework-5