2008-10-10 74 views
41

我收到以下错误,当我尝试调用包含SELECT语句的存储过程:“操作是无效的事务的状态”错误和交易范围

操作不适用于该交易

这里的状态是我的电话的结构:

public void MyAddUpdateMethod() 
{ 

    using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew)) 
    { 
     using(SQLServer Sql = new SQLServer(this.m_connstring)) 
     { 
      //do my first add update statement 

      //do my call to the select statement sp 
      bool DoesRecordExist = this.SelectStatementCall(id) 
     } 
    } 
} 

public bool SelectStatementCall(System.Guid id) 
{ 
    using(SQLServer Sql = new SQLServer(this.m_connstring)) //breaks on this line 
    { 
     //create parameters 
     // 
    } 
} 

与我创建另一个连接问题离子到交易中的同一个数据库?

回答

41

在做了一些研究后,似乎我不能用TransactionScope块向同一个数据库打开两个连接。我需要改变我的代码看起来像这样:

public void MyAddUpdateMethod() 
{ 
    using (TransactionScope Scope = new TransactionScope(TransactionScopeOption.RequiresNew)) 
    { 
     using(SQLServer Sql = new SQLServer(this.m_connstring)) 
     { 
      //do my first add update statement    
     } 

     //removed the method call from the first sql server using statement 
     bool DoesRecordExist = this.SelectStatementCall(id) 
    } 
} 

public bool SelectStatementCall(System.Guid id) 
{ 
    using(SQLServer Sql = new SQLServer(this.m_connstring)) 
    { 
     //create parameters 
    } 
} 
+0

我偶然发现了同样的情况。我必须在同一个事务范围内引用两个不同的数据库。谢谢你的提示。 – rageit 2012-04-09 12:52:32

1

我遇到了这个错误,当我的事务嵌套在另一个。存储过程是否有可能声明自己的事务,或者调用函数声明了它?

+0

我没有任何任何T-SQL交易代码我的存储过程。理论上,事务应该由MyAddUpdateMethod() – 2008-10-10 22:04:39

+0

+1控制,因为当我的存储过程中的事务语言错误时(即事务计数被搞砸),我收到了这个错误。 – codeMonkey 2017-03-28 16:48:05

7

我也碰到过同样的问题,我改变了事务超时15分钟,它的工作原理。 我希望这可以帮助。

TransactionOptions options = new TransactionOptions(); 
options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted; 
options.Timeout = new TimeSpan(0, 15, 0); 
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,options)) 
{ 
    sp1(); 
    sp2(); 
    ... 

} 
5

当我遇到这个异常时,出现了一个InnerException“Transaction Timeout”。由于这是在调试会话期间,当我在TransactionScope中暂停我的代码一段时间时,我选择忽略此问题。

当超时这个特定的异常出现在部署的代码,我认为在你config文件的以下部分将帮助你:

<system.transactions> 
     <machineSettings maxTimeout="00:05:00" /> 
</system.transactions>