允许在SQL事务期间读取?

时间:2013-04-18 20:03:51

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

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

enter image description here

现在你会注意到它们组织得很好。

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

这是问题所在。

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

例如:

 //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。

1 个答案:

答案 0 :(得分:2)

将您的使用更改为:

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