为什么我的方法在抛出异常时不会回滚?

时间:2015-03-04 12:10:04

标签: c# asp.net transactions informix transactionscope

为什么我的方法在发生异常时没有回滚?

我在RunAfterTrans发生异常时使用环境事务: 我发现插入processMethod已成功完成,InsertTrans已成功完成!


 public static int Insert(string processMethod, object[] processParameters, Type processType, object process, UserTransactionDTO transObj, string spPostConfirm, int toEmpNum)
        {
            int affectedRows = -7;
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    if (!string.IsNullOrEmpty(processMethod))
                    {

                        MethodInfo theMethod = processType.GetMethod(processMethod);
                        object res = theMethod.Invoke(process, processParameters);
                        transObj.ValuesKey = res.ToString();
                    }
                    affectedRows = InsertTrans(transObj);

                    if (affectedRows == 1)//Success
                    {
                        if (!string.IsNullOrEmpty(spPostConfirm))
                        {
                            affectedRows = RunPostConfirm(spPostConfirm, transObj.ValuesKey);
                            if (affectedRows == 0)
                            {
                                scope.Complete();//Commit
                                return 1;//Success
                            }
                            else
                            {
                                scope.Dispose();//Dispose
                                return -2;//Fail 
                            }
                        }
                        else
                        {
                            affectedRows = RunAfterTrans(transObj.TaskCode, transObj.OldStatusCode, transObj, toEmpNum);
                            if (affectedRows == 1)
                            {
                                scope.Complete();//Commit
                                return 1;//Success
                            }
                            else
                            {
                                scope.Dispose();//Dispose
                                return -3;//Fail
                            }
                        }
                    }
                    else
                    {
                        scope.Dispose();//Dispose
                        return -1;//Fail 
                    }
                }
                catch (Exception ee)
                {
                    scope.Dispose();//Dispose
                    return -1;//Fail 
                }

            }
        }

Invoke调用的方法,例如:


public string InsertRequest(EnhancementRequest obj)
    {
        using (IfxConnection con = new IfxConnection(ConfigurationManager.ConnectionStrings["rsmj"].ToString()))
        {
            int affectedRow = -1;
            int req_serial = 0;

            req_serial = GetMaxSerial(DateTime.Now.Year);
            StringBuilder cmdTxt = new StringBuilder();
            cmdTxt.Append(" INSERT INTO hqmtbl(req_ser,req_year,req_date,emp_num,requirement_type,problem_def,reference,objective,main_code,year) VALUES (?,?,?,?,?,?,?,?,?,? ) ");
            using (var myIfxCmd = new IfxCommand(cmdTxt.ToString(), con))
            {
                myIfxCmd.CommandType = CommandType.Text;
                myIfxCmd.Parameters.Add("req_ser", IfxType.Integer);
                myIfxCmd.Parameters.Add("req_year", IfxType.Integer);
                myIfxCmd.Parameters.Add("req_date", IfxType.DateTime);
                myIfxCmd.Parameters.Add("emp_num", IfxType.Integer);
                myIfxCmd.Parameters.Add("requirement_type", IfxType.SmallInt);
                myIfxCmd.Parameters.Add("problem_def", IfxType.LVarChar);
                myIfxCmd.Parameters.Add("reference", IfxType.NVarChar);
                myIfxCmd.Parameters.Add("objective", IfxType.LVarChar);
                myIfxCmd.Parameters.Add("main_code", IfxType.Integer);
                myIfxCmd.Parameters.Add("year", IfxType.Integer);

                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                myIfxCmd.Parameters[0].Value = ((object)req_serial) ?? DBNull.Value;
                myIfxCmd.Parameters[1].Value = DateTime.Now.Year;
                myIfxCmd.Parameters[2].Value = ((object)obj.ReqDate) ?? DBNull.Value;
                myIfxCmd.Parameters[3].Value = ((object)obj.EmpNum) ?? DBNull.Value;
                myIfxCmd.Parameters[4].Value = ((object)obj.RequirementType) ?? DBNull.Value;
                myIfxCmd.Parameters[5].Value = ((object)obj.ProblemDef) ?? DBNull.Value;
                myIfxCmd.Parameters[6].Value = ((object)obj.Reference) ?? DBNull.Value;
                myIfxCmd.Parameters[7].Value = ((object)obj.Objective) ?? DBNull.Value;
                myIfxCmd.Parameters[8].Value = ((object)obj.MainCode) ?? DBNull.Value;
                myIfxCmd.Parameters[9].Value = ((object)obj.Year) ?? DBNull.Value;
                affectedRow = myIfxCmd.ExecuteNonQuery();
            }
            con.Close();
            con.Dispose();
            if (affectedRow > 0)
            {
                return DateTime.Now.Year + "," + req_serial;
            }
            else if (affectedRow == 0)
            {
                return string.Empty;
            }
            else
            {
                return affectedRow.ToString();
            }

        }


    }

0 个答案:

没有答案