如何回滚分布式交易?

时间:2014-04-28 07:21:32

标签: sql asp.net-mvc-4 stored-procedures sql-server-2012

我编写了以下查询,当我执行下面的查询时,它会抛出错误'服务器'服务器上的MSDTC'不可用。但它在本地服务器上提交数据并且不在远程服务器上提交数据。所以我不想在两个数据库中提交数据,直到查询成功运行。如何在查询中处理它?<​​/ p>

SET XACT_ABORT ON;
BEGIN DISTRIBUTED TRANSACTION;
-- Delete candidate from local instance.
DELETE [DBASE].[OWNER].[TABLENAME]
    WHERE ID= 11;
-- Delete candidate from remote instance.
DELETE [SERVER2].[DBASE].[OWNER].[TABLENAME]
    WHERE ID= 10;
COMMIT TRANSACTION;
GO

1 个答案:

答案 0 :(得分:0)

您需要在查询中添加一些参数。

SET XACT_ABORT ON

BEGIN DISTRIBUTED TRANSACTION;
-- Delete candidate from local instance.
DELETE [DBASE].[OWNER].[TABLENAME]
    WHERE ID= 11;
-- Delete candidate from remote instance.
DELETE [SERVER2].[DBASE].[OWNER].[TABLENAME]
    WHERE ID= 10;

IF (XACT_STATE()) = -1
    ROLLBACK TRANSACTION

IF (XACT_STATE()) = 1
    COMMIT TRANSACTION;   

而且该错误(&#39;服务器上的MSDTC&#39;服务器&#39;不可用。&#39;)可能是一个难以解决的问题。 这是一个修复distributet transactions

的链接