Beispiel #1
0
 public void Transcation()
 {
     using (IRedisTransaction irt = this.iClient.CreateTransaction())
     {
         try
         {
             irt.QueueCommand(r => r.Set("key", 20));
             irt.QueueCommand(r => r.Increment("key", 1));
             irt.Commit(); // 提交事务
         }
         catch (Exception ex)
         {
             irt.Rollback();
             throw ex;
         }
     }
 }
Beispiel #2
0
 public void Transcation(Action action)
 {
     using (IRedisTransaction irt = this.iClient.CreateTransaction())
     {
         try
         {
             action.Invoke();
             irt.Commit(); // 提交事务
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
             irt.Rollback();
             throw ex;
         }
         finally {
             irt.Dispose();
         }
     }
 }
Beispiel #3
0
 /// <summary>
 /// Redis事务
 /// </summary>
 /// <param name="action">执行的方法</param>
 /// <param name="redisClient">导入redis客户端</param>
 public static void RedisContextInvoke(Action action, IRedisClient redisClient)
 {
     using (IRedisTransaction irt = redisClient.CreateTransaction())
     {
         try
         {
             action.Invoke();
             irt.Commit();
         }
         catch (Exception ex)
         {
             if (irt != null)
             {
                 irt.Rollback();
             }
             throw ex;
         }
         finally
         {
             irt.Dispose();
         }
     }
 }
Beispiel #4
0
        /// <summary>
        /// 单据审核成功后同步数据到HC网站
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="objects"></param>
        /// <returns></returns>
        public virtual HttpResponseResult OperateAfterAudit(Context ctx, List <DynamicObject> objects = null, IEnumerable <AbsSynchroDataInfo> datas = null, SynchroDataType dataType = default(SynchroDataType))
        {
            IEnumerable <AbsSynchroDataInfo> oDatas = null;
            IRedisClient       client = null;
            IRedisTransaction  trans  = null;
            HttpResponseResult result = null;
            Dictionary <List <string>, Dictionary <string, string> > dicts = null;

            if (objects != null && objects.Count > 0)
            {
                oDatas = GetK3Datas(ctx, objects, ref result);
            }
            if (datas != null && datas.Count() > 0)
            {
                oDatas = datas;
            }

            if (result == null)
            {
                result         = new HttpResponseResult();
                result.Success = true;
            }

            SqlCommand comm = null;
            bool       flag = true;

            try
            {
                if (oDatas != null && oDatas.Count() > 0)
                {
                    int    count = 0;
                    string sql   = GetExecuteUpdateSql(ctx, oDatas.ToList());

                    comm = UpdateBillInfos(ctx, oDatas.ToList(), sql, ref result);

                    if (comm != null)
                    {
                        count = comm.ExecuteNonQuery();
                    }

                    dicts = GetSynchroData(ctx, oDatas, dataType);

                    if (dicts != null && dicts.Count > 0)
                    {
                        RedisManager manager = new RedisManager(ctx);
                        client = manager.GetClientEx(ctx, GetDBID(ctx));
                        trans  = client.CreateTransaction();

                        foreach (var kv in dicts)
                        {
                            trans.QueueCommand(r => r.SetAll(kv.Value));

                            if (dataType == default(SynchroDataType))
                            {
                                trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["allKey"], kv.Key));
                                trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(this.DataType, this.Direction)["unreadKey"], kv.Key));
                            }
                            else
                            {
                                trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["allKey"], kv.Key));
                                trans.QueueCommand(r => r.AddRangeToSet(RedisKeyUtils.GetRedisSetKey(dataType, this.Direction)["unreadKey"], kv.Key));
                            }
                        }
                    }
                    else
                    {
                        result.Success = result.Success && true;
                    }


                    if (count > 0)
                    {
                        if (comm != null)
                        {
                            try
                            {
                                comm.Transaction.Commit();
                                flag = true;
                            }
                            catch (Exception ex)
                            {
                                flag            = false;
                                result.Success  = result.Success && false;
                                result.Message += ex.Message + Environment.NewLine + ex.StackTrace;
                                LogUtils.WriteSynchroLog(ctx, this.DataType, result.Message);
                            }
                        }
                    }
                    if (trans != null)
                    {
                        flag = trans.Commit();
                    }


                    if (!flag)
                    {
                        if (trans != null)
                        {
                            trans.Rollback();
                        }
                        if (comm != null)
                        {
                            if (comm.Transaction != null)
                            {
                                comm.Transaction.Rollback();
                            }
                        }
                        result.Success  = result.Success && false;
                        result.Message += string.Format("【{0}】单据编码【{1}】数据更新和同步到HC网站失败!", dataType == SynchroDataType.None ? DataType : dataType, string.Join(",", oDatas.Select(o => o.SrcNo).ToList()));
                        LogUtils.WriteSynchroLog(ctx, dataType == SynchroDataType.None ? DataType : dataType, result.Message);
                    }
                    else
                    {
                        foreach (var kv in dicts)
                        {
                            if (kv.Value != null && kv.Value.Count > 0)
                            {
                                LogHelper.WriteSynchroDataLog(ctx, this.DataType, client, kv.Value, true);
                            }
                        }

                        result.Success  = result.Success && true;
                        result.Message += string.Format("【{0}】单据编码【{1}】数据更新和同步到HC网站成功!", dataType == SynchroDataType.None ? DataType : dataType, string.Join(",", oDatas.Select(o => o.SrcNo).ToList()));
                        LogUtils.WriteSynchroLog(ctx, dataType == SynchroDataType.None ? DataType : dataType, result.Message);
                    }
                }
                else
                {
                    result.Success = result.Success && true;
                }
            }
            catch (Exception ex)
            {
                if (result != null)
                {
                    result.Success  = result.Success && false;
                    result.Message += ex.Message + Environment.NewLine + ex.StackTrace;
                    LogUtils.WriteSynchroLog(ctx, this.DataType, result.Message);
                }
                if (trans != null)
                {
                    trans.Rollback();
                }
                if (client != null)
                {
                    client.Dispose();
                }
                if (comm != null)
                {
                    if (comm.Transaction != null)
                    {
                        comm.Transaction.Rollback();
                    }
                }
            }

            finally
            {
                if (trans != null)
                {
                    trans.Dispose();
                }
                if (client != null)
                {
                    client.Dispose();
                }
                if (comm != null)
                {
                    if (comm.Connection != null)
                    {
                        comm.Connection.Close();
                    }
                }
            }

            return(result);
        }
Beispiel #5
0
 public void Roolback()
 {
     _tran.Rollback();
 }