具有OperationBehavior属性的方法中的存储过程调用:事务问题
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不查看存储过程,很难确切地知道发生了什么。我怀疑存储过程中存在一些错误,并且事务处理代码未正确执行。
这个SO问题似乎适用于您的情况: TransactionScope 和事务
在事务之外运行存储过程您可以将代码包装在抑制环境事务的
TransactionScope
中:作为最佳实践,我建议不要在存储过程中混合使用 .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:As a best practice, I would recommend to not mix .NET transactions and SQL transactions in stored procedures.