Exemple #1
0
 public static void ClearPool(string connectionString)
 {
     lock (_connectionPools)
     {
         Pool pool;
         if (_connectionPools.TryGetValue(connectionString, out pool))
         {
             pool.PoolVersion++;
             while (pool.Queue.Count > 0)
             {
                 UtlConnectionProxy target = pool.Queue.Dequeue().Target as UtlConnectionProxy;
                 if (target != null)
                 {
                     try
                     {
                         target.Close();
                         continue;
                     }
                     catch (Exception)
                     {
                         continue;
                     }
                 }
             }
         }
     }
 }
Exemple #2
0
 public static void ClearAllPools()
 {
     lock (_connectionPools)
     {
         foreach (KeyValuePair <string, Pool> pair in _connectionPools)
         {
             while (pair.Value.Queue.Count > 0)
             {
                 UtlConnectionProxy target = pair.Value.Queue.Dequeue().Target as UtlConnectionProxy;
                 if (target != null)
                 {
                     try
                     {
                         target.Close();
                         continue;
                     }
                     catch (Exception)
                     {
                         continue;
                     }
                 }
             }
             if (_poolVersion <= pair.Value.PoolVersion)
             {
                 _poolVersion = pair.Value.PoolVersion + 1;
             }
         }
         _connectionPools.Clear();
     }
 }
Exemple #3
0
        private static void ResizePool(Pool queue, bool forAdding)
        {
            int maxPoolSize = queue.MaxPoolSize;

            if (!forAdding && (maxPoolSize > 0))
            {
                maxPoolSize++;
            }
            else if (forAdding && (maxPoolSize > 0))
            {
                maxPoolSize--;
            }
            while (queue.Queue.Count > maxPoolSize)
            {
                UtlConnectionProxy target = queue.Queue.Dequeue().Target as UtlConnectionProxy;
                if (target != null)
                {
                    try
                    {
                        target.Close();
                        continue;
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }
            }
            maxPoolSize = queue.MinPoolSize;
            if (!forAdding && (maxPoolSize > 0))
            {
                maxPoolSize++;
            }
            else if (forAdding && (maxPoolSize > 0))
            {
                maxPoolSize--;
            }
            while (queue.Queue.Count < maxPoolSize)
            {
                UtlConnectionProxy target = new UtlConnectionProxy(new UtlConnectionOptions(queue.ConnectionString));
                target.Open();
                target.IsPooled = true;
                queue.Queue.Enqueue(new WeakReference(target, false));
                GC.KeepAlive(target);
            }
        }
Exemple #4
0
 public static void Add(string connectionString, UtlConnectionProxy hdl, int version)
 {
     lock (_connectionPools)
     {
         Pool pool;
         if ((_connectionPools.TryGetValue(connectionString, out pool) && (version == pool.PoolVersion)) && ((hdl.Created + hdl.LifeTime) > DateTime.UtcNow.Ticks))
         {
             ResizePool(pool, true);
             hdl.IsPooled = true;
             pool.Queue.Enqueue(new WeakReference(hdl, false));
             GC.KeepAlive(hdl);
         }
         else
         {
             try
             {
                 hdl.Close();
             }
             catch (Exception)
             {
             }
         }
     }
 }