【问题标题】:Will the page lock be released on committing nested transaction?提交嵌套事务时会释放页面锁吗?
【发布时间】:2012-07-23 08:07:16
【问题描述】:

众所周知,事务内部的数据库锁在事务结束时被释放。所以,在这段代码中..

public static TransactionScope CreateTransactionScope()
{
    return new TransactionScope(TransactionScopeOption.Required,
     new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted });
}

其实在这...

using (DataContext dataContext = new DataContext())
using (TransactionScope rootScope = CreateTransactionScope())
{
    using (TransactionScope nested = CreateTransactionScope())
    {
        Ticket ticket = dataContext.ExecuteQuery<Ticket>(
          "SELECT * FROM Tickets WITH (UPDLOCK) WHERE id={0}", ticketId).First();
        nested.Complete();
    }

    // Will the lock be still ON here? Because I don't need him to be!
}

何时释放行/页锁 (UPDLOCK) - 在处理嵌套事务或根事务之后?

【问题讨论】:

    标签: c# sql-server transactions locking transactionscope


    【解决方案1】:

    只有在根作用域退出 using 块并释放后才会释放。

    学习这类东西的最好方法是亲自动手。

    创建一个包含表 A 和表 B 的简单数据库,每个都包含一个列,将其命名为“TimeStamp”并创建插入时间戳(或任何类型的值)的简单控制台应用程序,您可以使用事务选项并学习行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多