每当异常在CLR从SSIS包调用的存储过程,抛出一个DTC事务中,客户端返回的错误(SSIS包),涉及到DTC而不是基本的例外。如何从CLR叫DTC交易从SSIS包存储过程中得到详细的错误
是否有可能有信息返回给客户端的底层错误?
注意:当存储过程从SQL Server Management Studio中运行分布式事务之外,则返回底层错误的详细信息。
Error: Executing the query "StoredProcedure” failed with the following error: “The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction”. Possible reasons: Problems with the query, “ResultSet” property not set correctly, parameters not set correctly, or connection not established correctly.
在单个SQL Server 2008实例上运行的所有代码。其再现问题
下面的代码说明了这个问题,但在标题中该客户所描述的情形不同
SSIS Package
---> Sequence Container (TransactionOption = Required)
---> Execute SQL Task (ADO.NET Connection Manager, SQLClient DataProvider)
---> SQL Server CLR Stored Procedure (No exception handling code)
---> TransactionScope(TransactionScopeOption.Required)
代码是C#控制台应用程序,而不是一个SSIS包
CLR存储过程
using System;
using System.Transactions;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void ExceptionStoredProcedure()
{
using (TransactionScope scope = new TransactionScope())
{
throw new Exception(@"Test exception thrown from ""ExceptionStoredProcedure""");
}
}
};
C#控制台应用程序客户机
using System;
using System.Data.SqlClient;
using System.Transactions;
namespace SQLCLRTester
{
class Program
{
static void Main(string[] args)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
string connectionString = "Data Source=.; Initial Catalog=Experiments; Integrated Security=Yes";
using (SqlConnection noOpConnection = new SqlConnection(connectionString))
{
noOpConnection.Open();
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand()
{
CommandText = "ExceptionStoredProcedure",
CommandType = System.Data.CommandType.StoredProcedure,
Connection = connection
};
connection.Open();
command.ExecuteNonQuery();
}
} scope.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught: {0}", ex.Message);
}
}
}
}
消息印刷通过客户端
捕获到异常:Microsoft分布式事务协调器(MS DTC)已取消分布式事务
注:
- 的InnerException是空
- 如果C# 控制台应用程序或CLR存储过程中的事务范围被删除,则不会创建DTC 事务并返回基础错误 的信息。
找到工作,感谢身边的文章链接提供的 鲍勃Beauchemin(合作伙伴,MVP) – AtTheMoment