具有OperationBehavior属性的方法中的存储过程调用:事务问题

发布于 2024-09-01 10:59:01 字数 553 浏览 3 评论 0原文

我正在使用 ADO.Net 的 ExecuteNonQuery 来调用存储过程,它的工作方式就像一个单独的魅力,但是在应该调用它的地方实现它时,我遇到了有关事务的问题。

例如

System.Data.SqlClient.SqlException: Transaction count after EXECUTE indicates a 
mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.

,之后还有一个超时。

我刚刚发现调用存储过程的方法标有以下 WCF 属性:

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]

这将如何影响我的存储过程的调用?我如何告诉 .Net 在该事务之外执行存储过程?

存储过程包含插入语句和事务,但删除它们不会改变行为......

I'm using ADO.Net's ExecuteNonQuery to call a stored procedure, works like a charm stand-alone but when implementing it where it should be called I'm running into problems concerning transactions.

For example

System.Data.SqlClient.SqlException: Transaction count after EXECUTE indicates a 
mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.

and also a timeout right after that.

I've just found out the method which calls the stored procedure is marked with the following WCF attribute:

[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]

How will this influence the call my stored procedure? How can I tell .Net to execute the stored procedure outside this transaction?

The stored procedure contains insert statements and also a transaction, but removing them doesn't change the behavior...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2024-09-08 10:59:01

如果不查看存储过程,很难确切地知道发生了什么。我怀疑存储过程中存在一些错误,并且事务处理代码未正确执行。

这个SO问题似乎适用于您的情况: TransactionScope 和事务

在事务之外运行存储过程您可以将代码包装在抑制环境事务的 TransactionScope 中:

using(TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) 
{
     // call SP
} 

作为最佳实践,我建议不要在存储过程中混合使用 .NET 事务和 SQL 事务。

It's hard to know exactly what is going on without seeing the stored procedure. I suspect there is some error in the stored procedure and the transaction handling code is not being executed properly.

This SO questions seems to apply to your situation: TransactionScope and Transactions

To run your stored procedure outside of a transaction you would wrap your code in a TransactionScope that suppresses the ambient transaction:

using(TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) 
{
     // call SP
} 

As a best practice, I would recommend to not mix .NET transactions and SQL transactions in stored procedures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文