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

问:

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

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

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

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

我的代码:

  public static int InsertGroups(List groups) { DBConnectionForInformix con = new DBConnectionForInformix(""); con.Open_Connection(); con.Begin_Transaction(); int affectedRow = -1; Dictionary groupsParameter = new Dictionary(); 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) { 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 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  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_NonQueryWithTransactionInsertGroups

并且Execute_NonQueryWithTransaction的返回用于返回错误代码和返回受影响的行。 但是在InsertGroups它仅被视为受影响的行。

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

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

  1. 一个只能抛出的拦截块是没有意义的,只会增加噪音。
  2. 只需使用exception进行error handling,所有正常返回都应指示成功。