跟踪分布式事务错误
本文关键字:错误 事务 分布式 跟踪 | 更新日期: 2023-09-27 18:14:01
如何追踪分布式传输错误?有什么方法可以确定所有连接是否仍然打开?我需要找到没有正确完成的交易?
当我从适配器填充表时,我偶尔会得到以下代码的分布式事务错误。我怀疑错误可能不在这段代码中,但这就是错误倾向于弹出的地方,只有在第二次尝试时,第一次总是工作正常。
using (OracleConnection oraConnection = new OracleConnection(connectionString)){
using (OracleCommand oraCommand = new OracleCommand( "DB_SCHEMA." + rmd.storedprocName, oraConnection )){
oraCommand.CommandType = CommandType.StoredProcedure;
if ( rmd.parameters != null ){
for ( int i = 0; i <= rmd.parameters.GetUpperBound( 0 ); i++ )
{
OracleParameter oraParameter = new OracleParameter();
oraParameter.ParameterName = rmd.parameterNames[ i ];
oraParameter.OracleDbType = rmd.parameterTypes[ i ];
oraParameter.Size = 15;
string dataType = oraParameter.OracleDbType.ToString().ToUpper();
switch ( dataType )
{
case "VARCHAR2":
oraParameter.Value = rmd.parameters[ i ].ToString();
break;
case "CHAR":
oraParameter.Value = rmd.parameters[ i ].ToString();
break;
case "NUMBER":
oraParameter.Value = Convert.ToDouble( rmd.parameters[ i ] );
break;
case "DATE":
oraParameter.Value = Convert.ToDateTime( rmd.parameters[ i ] );
break;
default:
oraParameter.Value = rmd.parameters[ i ].ToString();
break;
}
oraCommand.Parameters.Add( oraParameter );
}
}
oraCommand.Parameters.Add( "RS", Oracle.DataAccess.Client.OracleDbType.RefCursor).Direction = ParameterDirection.Output;
OracleDataAdapter oraDataAdapter = new OracleDataAdapter( oraCommand );
oraDataAdapter.Fill( dataTable ); //< -- error is thrown here
}
}
我不确定问题的确切来源,但我怀疑这是由于使用OracleDataAdapter
而您已经拥有OracleCommand
和OracleConnection
对象。
我认为使用OracleDataReader
来填充您的表并从连接中调用Open()
和Close()
方法会更干净。
当你把所有的参数赋给oraCommand
后,试试这个代替你的DataAdapter
:
oraConnection.Open();
OracleDataReader dr = oraCommand.ExecuteReader();
dataTable.Load(dr);
oraConnection.Close();
同样,我不认为这一定会"修复"您的事务错误,但它应该确保在执行存储过程时只有一个打开的连接,从而使连接更易于管理。
下面是一个使用TransactionScope
对象进行数据访问的例子:
try
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions(){IsolationLevel = IsolationLevel.ReadCommitted}))
try
{
CallYourDataAccessMethodsHere();
scope.Complete();
}
catch (Exception ex)
{
//Handle or log the error during the transaction
}
}
catch (Exception ex)
{
//This will catch a TransactionAbortedException and hopefully help track down the problem
}