简体   繁体   English

SQLException - 事务BEGIN / COMMIT不匹配

[英]SQLException - Transaction BEGIN / COMMIT mismatch

Have you encountered this exception for a stored procedure which does indeed have a balanced transaction block? 您是否遇到过确实具有平衡事务块的存储过程的此异常?

I double-checked the stored procedure and it has exactly one TRANSACTION BEGIN and cooresponding TRANSACTION END 我仔细检查了存储过程,它只有一个TRANSACTION BEGIN和cooresponding TRANSACTION END

Error logged 记录错误

SqlException - Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.  The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION. - Delete failed - stack:    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)     at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)     at System.Data.SqlClient.S ... [Rest of stack trace truncated by logging system]`

Additional Info 附加信息

The stored procedure does contain EXEC calls to another stored procedure. 存储过程确实包含对另一个存储过程的EXEC调用。 Would a mismatched transaction pair here cause the error to be surfaced in this way? 这里不匹配的事务对会导致错误以这种方式浮出水面吗?

Update It turns out that there was a violation of a foreign key constraint within the nested stored procedure. 更新事实证明,嵌套存储过程中存在违反外键约束的情况。 The outer transaction did not include a Try/Catch block and had SET XACT_ABORT ON specified, which did not properly handle either a commit or rollback. 外部事务不包含Try / Catch块并且指定了SET XACT_ABORT ON ,它无法正确处理提交或回滚。 Also added a check for @@TransactionCount > 0 before attempting a rollback 在尝试回滚之前,还添加了对@@ TransactionCount> 0的检查

Yes it would. 是的,它会的。 Each BEGIN increments @@trancount , each commit decrements it. 每个BEGIN增加@@trancount ,每个提交减少它。 Only when the count gets to 0 is the transaction really committed. 只有当计数达到0时,事务才真正提交。 Your procedure, as a caller, cannot control this. 作为呼叫者,您的程序无法控制此操作。 It is the job of the called procedures to behave properly and balance the BEGIN and COMMIT count, if any of the called procedures has a imbalance, you'll see this error. 调用过程的作用是正常运行并平衡BEGIN和COMMIT计数,如果任何被调用的过程有不平衡,您将看到此错误。

Are you sure you don't have path that produces this 你确定你没有产生这个的路径吗?

BEGIN TRAN

ROLLBACK TRAN

COMMIT TRAN

Yes, you're going down the right path. 是的,你走的是正确的道路。 If a nested procedure call creates transactions, they affect the calling procedure. 如果嵌套过程调用创建事务,则它们会影响调用过程。

Check that other procedure 检查其他程序

Make sure you don't have inadvertently written 确保你没有无意中写过

 return
 commit

in place of 代替

 commit
 return

For me, that was the problem. 对我来说,这就是问题所在。

Add this on top of PROCEDURE creation text 在PROCEDURE创建文本之上添加它

SET XACT_ABORT ON; SET XACT_ABORT ON;

It will ensure that if nothing got executed, the transaction is aborted entirely. 它将确保如果没有执行任何操作,则事务将完全中止。

MSDN Doc: http://technet.microsoft.com/en-us/library/ms188792(v=sql.105).aspx MSDN Doc: http//technet.microsoft.com/en-us/library/ms188792(v = sql.105).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在查询中使用参数会导致BEGIN / COMMIT不匹配SQLException - Using parameter in query results in BEGIN / COMMIT mismatch SQLException 开始交易…提交交易问题 - Begin Transaction … Commit Transaction Issue SQL事务开始结束不匹配? - SQL TRANSACTION Begin End mismatch? COMMIT TRANSACTION请求没有相应的BEGIN TRANSACTION - The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION 此存储过程中的开始/提交不匹配在哪里? - Where is the Begin/Commit mismatch in this stored procedure? IMPLICIT TRANSACTIONS ON / OFF和BEGIN / COMMIT交易 - IMPLICIT TRANSACTIONS ON/OFF and BEGIN /COMMIT Transaction EXECUTE之后的事务计数表明BEGIN和COMMIT语句的数量不匹配 - Transaction count after EXECUTE indicates mismatching number of BEGIN and COMMIT statements EXECUTE之后的事务计数表明BEGIN和COMMIT?的数量不匹配? - Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT? java.sql.SQLException的解决方案:无法在全局事务中使用本地事务提交 - Solution for java.sql.SQLException: could not use local transaction commit in a global transaction 消息3902,级别16,状态1. COMMIT TRANSACTION请求没有相应的BEGIN TRANSACTION - Msg 3902, Level 16, State 1. The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM