这个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_NonQueryWithTransaction
和InsertGroups
。
并且Execute_NonQueryWithTransaction
的返回用于返回错误代码和返回受影响的行。 但是在InsertGroups
它仅被视为受影响的行。
您是否可以将Execute_NonQueryWithTransaction
(因此事务回滚)中的错误代码视为InsertGroups
成功(插入行),然后提交失败?
总体而言,代码需要进行大量清理:
- 一个只能抛出的拦截块是没有意义的,只会增加噪音。
- 只需使用exception进行error handling,所有正常返回都应指示成功。
- 烦人的SQLexception,可能是由于某些代码做错了
- Owin / Katana应该取代Web API吗?
- 在学习ASP.NET和Sharepoint之前,我应该学习.NET和C#吗?
- 指定asp.net核心1.0 WebAPI.exe应该在program.cs中为prod和dev使用的url(端口)
- 如何将事件处理程序附加到在运行时创建的ASP.NET控件?
- 发布错误:无法加载文件或程序集“Microsoft.Web.XmlTransform”,Version = 1.4.0.0,Culture = neutral等等或其依赖项之一
- 如何为ASP.NET 5应用程序设置区域设置?
- 在浏览器中加载虚拟目录名时,会话在AcquireRequestState中为空,但在加载Default.aspx时不为null
- 无法生成Crystal报表 – 登录参数不正确