c#-此IfxTransaction已完成;它不再可用

问:

当我使用事务时,每100条记录中大约有1条会出现以下错误.

This IfxTransaction has completed; it
is no longer usable

我无法预期何时发生错误或此错误的原因是什么.

我尝试在同一笔交易中插入大约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;
        }
最佳答案
您似乎正在处理错误并在两个位置回滚事务(在Execute_NonQueryWithTransaction和InsertGroups中).

从Execute_NonQueryWithTransaction返回的结果既用于返回错误代码,又用于返回受影响的行.但是在InsertGroups中,仅将其作为受影响的行进行检查.

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

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

>只能抛出的挡块毫无意义,只会增加噪音.
>仅将异常用于错误处理,所有正常返回都应指示成功.

转载注明原文:c#-此IfxTransaction已完成;它不再可用 - 代码日志