多的TransactionScope问题 [英] Multiple TransactionScope problem

查看:313
本文介绍了多的TransactionScope问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个事务,写一些数据的子事务,读回数据,并回滚事务。

I want to create a transaction, writing some data in a sub-transaction, reading the data back, and rollback the transaction.

using(var transaction = new TransactionScope()) 
{
     using(var transaction = new TransactionScope()) 
     {
          // save data via LINQ / DataContext
          transaction.Complete();
     }
     // Get back for assertions
     var tempItem = // read data via LINQ / DataContext THROWS EXCEPTION
}

但是在读我得到的。System.Transactions.TransactionException:操作是无效的事务的状态

我应该如何设置事务属性来避免这种情况?

How should I set transaction properties to avoid this?

推荐答案

该异常不能在没有完整的堆栈跟踪调试。它具有不同的含义取决于上下文。通常这意味着你做的事情你不应该在事务内,但没有看到分贝来电或堆栈跟踪任何人都可以做的是猜测。一些常见的原因我知道(这绝不是COM prehensive我敢肯定)包括:

This exception cannot be debugged without the full stack trace. It has a different meaning depending on the context. Usually it means you're doing something you shouldn't inside the transaction, but without seeing db calls or stack trace all anybody can do is guess. Some common causes I know of (and this is by no means comprehensive I'm sure) include:

  1. 中嵌套的TransactionScope 访问多个数据源(例如不同的连接字符串)。这将导致推广分布式事务,如果你没有运行DTC就会失败。答案通常的没有的启用DTC,而是要清理您的交易或包装与其他数据访问一个新的的TransactionScope(TransactionOptions.RequiresNew)
  2. 的TransactionScope
  3. 中未处理的异常
  4. 违反了隔离级别的任何操作,比如试图读取刚刚插入的行/更新。
  5. 在SQL死锁;交易甚至可以自己死锁在某些情况下,但如果#1适用,隔离等老年退休金计划进入新的交易可能导致死锁,如果你不小心。
  6. 事务超时。
  7. 从数据库中的任何其它错误。
  1. Accessing multiple data sources (ie different connection strings) within a nested TransactionScope. This causes promotion to a distributed transaction and if you do are not running DTC it will fail. The answer is usually not to enable DTC, but to clean up your transaction or wrap the other data access with a new TransactionScope(TransactionOptions.RequiresNew).
  2. Unhandled exceptions within the TransactionScope.
  3. Any operation that violates the isolation level, such as trying to read rows just inserted/updated.
  4. SQL deadlocks; transactions can even deadlock themselves in certain cases, but if #1 applies, isolating other ops into new transactions can cause deadlocks if you're not careful.
  5. Transaction timeouts.
  6. Any other error from the database.

我绝对不知道每一个可能的原因,但如果您发布完整的堆栈跟踪,并在code中的实际分贝呼吁我来看看,让你知道,如果我看不到任何东西。

I definitely don't know every possible cause, but if you post the complete stack trace and the actual db calls in your code I'll take a look and let you know if I see anything.

这篇关于多的TransactionScope问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆