这个IfxTransaction已经完成;它不再可用

时间:2011-06-26 10:20:51

标签: c# asp.net transactions informix

问:

当我使用这些交易时,我会在每100条记录中约有1条出现以下错误。

  

此IfxTransaction已完成;它   已不再可用

我不能指望错误何时发生或错误的原因是什么。

我尝试在同一个交易中插入约607条记录。

我的代码:

 public static int InsertGroups(List<Group> groups)
        {
            DBConnectionForInformix con = new DBConnectionForInformix("");
            con.Open_Connection();
            con.Begin_Transaction();

            int affectedRow = -1;
            Dictionary<string, string> groupsParameter = new Dictionary<string, string>();
            try
            {
                foreach (Group a in groups)
                {
                    groupsParameter.Add("id", a.GroupId.ToString());
                    groupsParameter.Add("name", a.Name);
                    groupsParameter.Add("studentcount", a.StudentCount.ToString());
                    groupsParameter.Add("divisiontag", a.DivisionTag.ToString());
                    groupsParameter.Add("entireclass", a.EntireClass.ToString());
                    groupsParameter.Add("classid", a.ClassId.ToString());
                    groupsParameter.Add("depcode", a.DepCode.ToString());
                    groupsParameter.Add("studycode", a.StudyCode.ToString());
                    groupsParameter.Add("batchnum", a.BatchNum.ToString());
                    affectedRow = DBUtilities.InsertEntityWithTrans("groups", groupsParameter, con);
                    groupsParameter.Clear();
                    if (affectedRow < 0)
                    {
                        break;
                    }
                }

                if (affectedRow > 0)
                {
                    con.current_trans.Commit();
                }
            }
            catch (Exception ee)
            {
                string message = ee.Message;
            }

            con.Close_Connection();
            return affectedRow;

        }

 public void Begin_Transaction()
        {
            if (this.connection.State == ConnectionState.Open)
            {
                this.current_trans = this.connection.BeginTransaction(IsolationLevel.Serializable);
            }
        }

public static int InsertEntityWithTrans(string tblName, Dictionary<string, string> dtParams, DBConnectionForInformix current_conn)
        {
            int Result = -1;
            string[] field_names = new string[dtParams.Count];
            dtParams.Keys.CopyTo(field_names, 0);
            string[] field_values = new string[dtParams.Count];
            string[] field_valuesParam = new string[dtParams.Count];
            dtParams.Values.CopyTo(field_values, 0);
            for (int i = 0; i < field_names.Length; i++)
            {
                field_valuesParam[i] = "?";
            }
            //----------------------------------------------------------------------------------------------------------------------------------------------
            string insertCmd = @"INSERT INTO " + tblName + " (" + string.Join(",", field_names) + ") values (" + string.Join(",", field_valuesParam) + ")";
            //----------------------------------------------------------------------------------------------------------------------------------------------

            IfxCommand com = new IfxCommand(insertCmd);
            for (int j = 0; j < field_names.Length; j++)
            {
                com.Parameters.Add("?", field_values[j]);
            }
            try
            {

                Result = current_conn.Execute_NonQueryWithTransaction(com);
                if (current_conn.connectionState == ConnectionState.Open && Result > 0)//OK: logging
                {
                    # region // Log Area

                    #endregion
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return Result;
        }

public int Execute_NonQueryWithTransaction(IfxCommand com)
        {
            string return_msg = "";
            int return_val = -1;
            Open_Connection();
            com.Connection = this.connection;
            com.Transaction = current_trans;
            try
            {
                return_val = com.ExecuteNonQuery();

            }
            catch (IfxException ifxEx)// Handle IBM.data.informix : mostly catched
            {
                return_val = ifxEx.Errors[0].NativeError;
                return_msg = return_val.ToString();
            }
            catch (Exception ex)// Handle all other exceptions.
            {
                return_msg = ex.Message;
            }
            finally
            {
                if (!string.IsNullOrEmpty(return_msg))//catch error
                {
                    //rollback
                    current_trans.Rollback();
                    Close_Connection();
                    connectionstate = ConnectionState.Closed;
                }

            }
            return return_val;
        }

1 个答案:

答案 0 :(得分:3)

您好像在两个地方处理错误并回滚交易(Execute_NonQueryWithTransactionInsertGroups

来自Execute_NonQueryWithTransaction的返回用于返回错误代码和返回受影响的行。但是在InsertGroups中,它被纯粹视为受影响的行。

您是否可以在Execute_NonQueryWithTransaction中将InsertGroups的错误代码(因此事务回滚)视为成功(插入行),然后提交失败?

整体而言,代码需要进行大量清理:

  1. 只能投掷的拦截块毫无意义,只会增加噪音。
  2. 只需使用异常进行错误处理,所有正常返回都应表示成功。