此IfxTransaction已完成;它不再可用

本文关键字:不再 IfxTransaction 已完成 | 更新日期: 2023-09-27 18:00:29

Q:

当我使用事务时,大约每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;
        }

此IfxTransaction已完成;它不再可用

您似乎在处理错误并在两个位置回滚事务(在Execute_NonQueryWithTransactionInsertGroups中)。

Execute_NonQueryWithTransaction的返回既用于返回错误代码,也用于返回受影响的行。但在InsertGroups中,它纯粹作为受影响的行进行检查。

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

总体而言,代码需要大量清理:

  1. 一个只投的接球盖帽毫无意义,只会增加噪音
  2. 只要使用异常进行错误处理,所有正常的返回都应该表示成功