【问题标题】:Allow reading during an SQL transaction?允许在 SQL 事务期间读取?
【发布时间】:2013-04-12 02:32:40
【问题描述】:

在我的网站上,我有一个活动日历:

现在您会注意到它们按天组织得很好。

这是管理员用来组织项目的功能。

这就是问题所在。

发生这种情况时,我使用事务范围,因为它会修改多个事件并且可能会失败:

例如:

 //needs id '9999'
public string Explode()
{
   string eid = Request.Form["id"];

   using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new System.TimeSpan(2, 45, 0)))
            {
   try
   {
      int id = int.Parse(eid);

      Project p = ProjectManager.GetProject(id);
      int resID = p.Schedule.Employee.EmployeID;
      Employee e = ResourceManager.GetEmployee(resID);
      if (e == null)
         throw new Exception();

      var tasks = ProjectManager.GetProjectTasks(id);
      var ordered = SyncManager.Instance.GetTasks((int)p.PaymoID);

      Task firstTask = null;

      foreach (Paymo.Task t in ordered)
      {
         Task tsk = (from obj in tasks where obj.PaymoID != null && obj.PaymoID.Value == t.ID select obj).FirstOrDefault();
         if (tsk != null && t.UserName == e.EmployeName
            && tsk.Schedule == null)
         {
            if (firstTask == null)
            {
               firstTask = tsk;
            }

            int scheduleID = ScheduleManager.CreateSchedule(p.Schedule.DateFrom.Value
               ,t.BudgetHours <= 0 ? 1.0M : t.BudgetHours , e.EmployeID);

            if (scheduleID == -1)
               throw new Exception();

            if (!ScheduleManager.ChangeTaskSchedule(tsk.TaskID, scheduleID))
               throw new Exception();

            if (!ScheduleManager.GiveLowestPriority(ScheduleManager.GetSchedule(scheduleID)))
            {
               throw new Exception();
            }                           
          }
       }

       ScheduleManager.ChangeProjectSchedule(p.ProjectID, null);
       if (firstTask != null)
       {
          Bump b = new Bump();
          b.DoBump(firstTask.Schedule);
       }
       scope.Complete();
    }

    catch (Exception)
    {
       return "fail";
    }
  } 
  return "";
}

现在的问题是,当事务发生时,如果用户试图以只读模式访问日历,他们必须等待才能看到它。

在交易过程中网站是否仍可以通过某种方式加载? 我使用 LINQ SQL 和 SQL Server 2008 和 VS 2012。

【问题讨论】:

  • 好吧,我不知道 LINQ 可能会对这种策略施加什么荒谬的限制,但是您可以使用已提交的读取快照隔离来执行读取,这将防止它们被写入器阻止 - 假设这是您想要的.如果有人要更改日程,读者可以看到更改前的版本,还是让他们等待更新?
  • 他们可以看到预更改版本...我不希望他们不得不等待...
  • 这并不能真正回答@AaronBertrand 的问题。显然,您不希望他们等待 - 但您是否希望他们能够读取预先更改的版本(即脏读,尽管是反向的:D)

标签: c# asp.net sql-server sql-server-2008


【解决方案1】:

将您的使用更改为:

new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted,Timeout = new System.TimeSpan(2, 45, 0) }));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多