嵌套的TransactionScope异常

时间:2012-10-02 02:13:10

标签: c# nested transactionscope

以下代码抛出以下异常。我做错了什么?

错误堆栈:System.Transactions.TransactionAbortedException:事务已中止。 ---> System.Data.SqlClient.SqlException:无法执行事务操作,因为存在处理此事务的挂起请求。

public class NestedTransaction
{
    public void SomeTransaction()
    {
        using (var context = new SomeDBContext())
        {
            using (TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required,
                  new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
             {
                context.Connection.Open();

                List<SomeEntities> entities = GetEntitiesFromDatabase();

                    entities.ForEach(entity =>
                    {
                        SaveEntity((EntityConnection)context.Connection, entity);                            
                    });

            context.SaveChanges();

            context.Connection.Close();

            transaction.Complete();

            }               
        }
    }

    private static void SaveEntity(EntityConnection connection,SomeEntity entity)
    {
        foreach (SomeEntity childEntity in entity.ChildEntities)
        {
              SaveEntity(connection, childEntity)
        }
    }
}

提前致谢 二重

0 个答案:

没有答案