跟踪分布式事务错误

时间:2013-05-16 16:27:58

标签: c# oracle database-connection distributed-transactions

如何追踪分布式传输错误?有没有办法确定所有连接仍然是开放的?我需要找到未正确关闭的交易吗?

当我从适配器填充表时,我偶尔会遇到以下代码的分布式事务错误。我怀疑错误可能不在此代码中,但这是错误趋向于弹出的地方,仅在第二次尝试时,第一次始终正常。

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
 }
}

1 个答案:

答案 0 :(得分:0)

我不确定问题的确切位置,但我怀疑这是因为您已经拥有OracleDataAdapterOracleCommand对象时使用了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
    }