예제 #1
0
 public void ExecuteTrans(IDbHelpParam pDbHelpParam, DbHelp.ExecuteTransHandler pTrands, WZ.Common.DbHelp.ITransM pObj)
 {
     throw new NotImplementedException("事务不能嵌套使用");
 }
예제 #2
0
        /// <summary>
        /// 事务处理
        /// </summary>
        /// <param name="pDbHelpParam">DbHelp 接收参数专用类</param>
        /// <param name="pTrands">要处理事务的方法</param>
        /// <param name="pObj">传参数</param>
        public void ExecuteTrans(IDbHelpParam pDbHelpParam, WZ.Common.DbHelp.ExecuteTransHandler pTrands, WZ.Common.DbHelp.ITransM pObj)
        {
            IDbConnection conn = pDbHelpParam.DbProvider.NewConnection();

            try
            {
                using (IDbCommand cmd = conn.CreateCommand())
                {
                    IDbTransaction trans;
                    SetCmd(pDbHelpParam, cmd, conn);
                    trans           = conn.BeginTransaction();
                    cmd.Transaction = trans;

                    try
                    {
                        int returnTrands = pTrands(new TransHelp(cmd), pObj);//执行业务逻辑

                        if (returnTrands == 1)
                        {
                            trans.Commit();//提交事务
                        }
                        else
                        {
                            trans.Rollback();//回滚事务
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            trans.Rollback();//回滚事务
                            pObj.returnValue = ex.Message;
                        }
                        catch (Exception exTrans)
                        {
                            pObj.returnValue = ex.Message + " " + exTrans.Message;
                            throw new Exception(ex.Message + " " + exTrans.Message + ":回滚事务出错");
                        }
                    }

                    cmd.Parameters.Clear();
                }
            }
            catch (Exception ex)
            {
                pDbHelpParam.DbProvider.Dispose(conn);
                throw new Exception(ex.Message + "Update sql:" + pDbHelpParam.SQL);
            }
            finally
            {
                pDbHelpParam.DbProvider.Dispose(conn);
            }
        }