object pooling and connection pooling in ASP.Net / C#

In Object pooling, you can control the number of connections.

In connection pooling, you can control the maximum number reached.

When using connection pooling, if there is nothing in the pool, a connection is created since the creation is on the same thread.

In object pooling, the pool decides the creation of an object depending on whether the maximum is reached which in case if it is, the next available object is returned. However, this could increase the time complexity if the object is heavy.

Object pooling allows you to control the number of connections being used, whereas connection pooling allows the control of maximum number reached.


Object Pool in C#.NET:

Creating pool manager.

public sealed class MyPoolManager
{
       private Queue poolQ = new Queue();
       private Hashtable tblPool = new Hashtable();
       private static readonly object objLock = new object();
       private const int PoolSize = 10;
       private int count = 0;
       private static MyPoolManager poolManager = null;
       ///

       /// Private constructor to prevent instantiation
       ///

       private MyPoolManager()
       {
       }

    static MyPoolManager()
     {
          poolManager = new MyPoolManager();
     }

      public static MyPoolManager Instance
      {
           get
           {
                if (poolManager != null)
                {
                     return poolManager;
                }
                      return null;
            }
       }

      public void CreatePoolObjects(object obj)
      {
             object _obj = obj;
             count = 0;
             poolQ.Clear();
             tblPool.Clear();

             for (int objCounter = 0; objCounter < PoolSize; objCounter++)
             {
                  _obj = new object();

                  lock (objLock)
                  {
                        tblPool.Add(_obj.GetHashCode(), _obj);
                        poolQ.Enqueue(_obj);
                        count++;
                   }
              }
       }

       public bool AddObjectToPool(object obj)
       {
            if (count == PoolSize)
                 return false;

             lock (objLock)
             {
                   tblPool.Add(obj.GetHashCode(), obj);
                   poolQ.Enqueue(obj);
                   count++;
             }
                    return true;
         }

       public object ReleaseObjectFromPool()
       {
            if (count == 0)
                  return null;

             lock (objLock)
             {
                   tblPool.Remove(poolQ.Dequeue().GetHashCode());
                   count--;
                   if (poolQ.Count > 0)
                           return poolQ.Dequeue();
              }
                    return null;
         }

       public object ReleaseObjectFromPool(object obj)
       {
                if (count == 0)
                        return null;

                lock (objLock)
                {
                        tblPool.Remove(obj.GetHashCode());
                        count--;
                        PutInPoolAgain();
                        return obj;
               }
       }

       private void PutInPoolAgain()
       {
               if (poolQ.Count > 0)
                         poolQ.Clear();

               foreach (int key in tblPool.Keys)
               {
                      poolQ.Enqueue(tblPool[key]);
               }
       }

        public int CurrentObjectsInPool
        {
               get
               {
                    return count;
               }
         }

        public int MaxObjectsInPool
        {
                  get
                  {
                          return PoolSize;
                  }
         }
}


using MyPoolManager
object obj1 = new object();
object obj2 = new object();
MyPoolManager poolManager = MyPoolManager.Instance;
poolManager.AddObjectToPool(obj1);
poolManager.AddObjectToPool(obj2);
MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());
poolManager.ReleaseObjectFromPool(obj1);
MessageBox.Show(poolManager.CurrentObjectsInPool.ToString());
object obj = null;
for(;;)
{
       obj = poolManager.ReleaseObjectFromPool();
       if(obj != null)
            MessageBox.Show(obj.GetHashCode().ToString());
       else
       {
            MessageBox.Show("No more objects in the pool");
            break;
       }
}
Tags:
Hot on Web:


About author