3

您好我正在使用 Entity Framework 4.1 代码优先方法。我有MyContainer继承的类DBContext

我有一个流程,每个步骤有 7 个步骤访问许多存储库方法(大约 60 个)。此流程自动或手动执行取决于业务逻辑和用户要求。现在为了自动进程的性能观点,我创建了上下文,即MyContainer一次对象并将其传递给所有方法,并在进程结束时处理它,它的工作正常并提高了性能。但是当这个进程手动执行时,执行相同的方法并且容器是在方法本身中创建和处置的。例如下面,但这只是粗略的代码。

public bool UpdateProcess(Process process, MyContainer container = null)
    {
        bool disposeContainer = false;
        if (container == null)
        {
            container = new MyContainer();
            disposeContainer = true;
        }
        var result = SaveProcess(container, process);
        container.SaveChanges();
        if (disposeContainer)
            container.Dispose();
        return result;
    }

对于自动流程,事务在流程开始时创建并在流程结束时结束,手动事务在根据用户在 ui 上的操作调用的方法中的 bll 处创建。现在假设我的自动流程正在运行同时用户在 ui 上做了一些操作我得到了异常"Transaction (Process ID 65)被锁定资源与另一个进程并已被选择“当从手动和自动过程一起调用 UpdateProcess() 方法时,我在 container.SaveChanges() 上得到它.

任何帮助将不胜感激。

如果我在这个存储库方法中创建一个事务范围,比如

public bool UpdateProcess(Process process, MyContainer container = null)
 {         bool disposeContainer = false;        
           if (container == null)  
           {             
                  container = new MyContainer();         
                  disposeContainer = true;         
            }
       using(var trans=new TransactionScop(TransactionScopeOption.RequiresNew))        
       { 
          var result = SaveProcess(container, process);
          container.SaveChanges();
          trans.Complete();
       }        
          if (disposeContainer)           
          container.Dispose();        
          return result;   
 } 

它工作正常。但是我不想在存储库中创建事务,因为事务已经在 bll 中进行。

任何帮助都会得到帮助。

4

1 回答 1

2

The issue (sql deadlocking) you are having is common to most non-trivial client - database systems and can be very difficult to resolve.

The primary rule in designing code where deadlocks may occur is to assume that they will and design the application to handle them appropriately. This is normally handled via resubmitting the transaction. As documented by microsoft here

Although deadlocks can be minimized, they cannot be completely avoided. That is why the front-end application should be designed to handle deadlocks.

In order to minimise any deadlocks you are seeing the normal approach I take is as follows:

  1. Run profiler with a deadlock graph enabled to capture all deadlocks
  2. Export all deadlocks to text and investigate why they are occurring
  3. Check to see these can be minimised by using table hints and/or reducing the transation isolation level
  4. See if they can be minimised by other means, e.g. changing the order of operation
  5. Finally after all this has been completed ensure that you are trapping for deadlocks anytime you communicate with the database & resubmit the transaction / query etc.
于 2012-05-21T12:38:56.223 回答