Beispiel #1
0
 public Executer(IDistributedCache cache, string masterConnectionString, string[] slaveConnectionStrings, ILogger log)
 {
     Log        = log;
     MasterPool = new NpgsqlConnectionPool("主库", masterConnectionString, null, null);
     Cache      = cache;
     if (slaveConnectionStrings != null)
     {
         foreach (var slaveConnectionString in slaveConnectionStrings)
         {
             var slavePool = new NpgsqlConnectionPool($"从库{SlavePools.Count + 1}", slaveConnectionString, () => Interlocked.Decrement(ref slaveUnavailables), () => Interlocked.Increment(ref slaveUnavailables));
             SlavePools.Add(slavePool);
         }
     }
     if (cache != null)
     {
         var key1 = $"testCacheSupportMultiRemove{Guid.NewGuid().ToString("N")}";
         var key2 = $"testCacheSupportMultiRemove{Guid.NewGuid().ToString("N")}";
         cache.Set(key1, new byte[] { 65 });
         cache.Set(key2, new byte[] { 65 });
         try { cache.Remove($"{key1}|{key2}"); } catch { }                 // redis-cluster 不允许执行 multi keys 命令
         CacheSupportMultiRemove = cache.Get(key1) == null && cache.Get(key2) == null;
         if (CacheSupportMultiRemove == false)
         {
             log.LogWarning("PSqlHelper Warning: 低性能, IDistributedCache 没实现批量删除缓存 Cache.Remove(\"key1|key2\").");
             CacheRemove(key1, key2);
         }
     }
 }
Beispiel #2
0
        void LoggerException(NpgsqlConnectionPool pool, NpgsqlCommand cmd, Exception e, DateTime dt, string logtxt, bool isThrowException = true)
        {
            if (IsTracePerformance)
            {
                TimeSpan ts = DateTime.Now.Subtract(dt);
                if (e == null && ts.TotalMilliseconds > 100)
                {
                    Log.LogWarning($"{pool.Policy.Name}执行SQL语句耗时过长{ts.TotalMilliseconds}ms\r\n{cmd.CommandText}\r\n{logtxt}");
                }
            }

            if (e == null)
            {
                return;
            }
            string log = $"{pool.Policy.Name}数据库出错(执行SQL)〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓\r\n{cmd.CommandText}\r\n";

            foreach (NpgsqlParameter parm in cmd.Parameters)
            {
                log += parm.ParameterName.PadRight(20, ' ') + " = " + (parm.Value ?? "NULL") + "\r\n";
            }

            log += e.Message;
            Log.LogError(log);

            RollbackTransaction();
            cmd.Parameters.Clear();
            if (isThrowException)
            {
                throw e;
            }
        }