Exemplo n.º 1
0
 public object BorrowObject()
 {
     lock (sync)
     {
         if (closed)
         {
             throw new InvalidOperationException("The pool is closed.");
         }
         object useable = null;
         IEnumerator <object> objects          = passiveObjects.GetEnumerator();
         List <object>        destroyedObjects = new List <object>();
         while (objects.MoveNext() && useable == null)
         {
             PoolableObjectFactory.ActivateObject(objects.Current);
             if (PoolableObjectFactory.ValidateObject(objects.Current))
             {
                 useable = objects.Current;
             }
             else
             {
                 PoolableObjectFactory.DestroyObject(objects.Current);
                 destroyedObjects.Add(objects.Current);
             }
         }
         passiveObjects.RemoveAll(o => destroyedObjects.Contains(o));
         if (useable == null)
         {
             useable = PoolableObjectFactory.MakeObject();
             PoolableObjectFactory.ActivateObject(useable);
         }
         activeObjects.Add(useable);
         return(useable);
     }
 }
Exemplo n.º 2
0
        public T BorrowObject(long maxWaitMillisecs)
        {
            CheckClosed();

            PooledObject <T> p = null;
            bool             blockWhenExhausted = this.blockWhenExhausted;
            bool             create;

            while (p == null)
            {
                create = false;
                if (blockWhenExhausted)
                {
                    p = idleObjects.PollFirst();
                    if (p == null)
                    {
                        create = true;
                        p      = Create();
                    }

                    if (p == null)
                    {
                        if (maxWaitMillisecs < 0)
                        {
                            p = idleObjects.TakeFirst();
                        }
                        else
                        {
                            p = idleObjects.PollFirst(TimeSpan.FromMilliseconds(maxWaitMillisecs));
                        }
                    }

                    if (p == null)
                    {
                        throw new NoSuchElementException("Timeout waiting for idle object");
                    }

                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }
                else
                {
                    p = idleObjects.PollFirst();
                    if (p == null)
                    {
                        create = true;
                        p      = Create();
                    }

                    if (p == null)
                    {
                        throw new NoSuchElementException("Pool exhausted");
                    }

                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }

                if (p != null)
                {
                    try
                    {
                        factory.ActivateObject(p.TheObject);
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            Destroy(p);
                        }
                        catch
                        {
                            // Ignore - activation failure is more important
                        }
                        p = null;
                        if (create)
                        {
                            NoSuchElementException nsee = new NoSuchElementException(
                                "Unable to activate object", e);
                            throw nsee;
                        }
                    }

                    if (p != null && TestOnBorrow)
                    {
                        bool      validate            = false;
                        Exception validationThrowable = null;
                        try
                        {
                            validate = factory.ValidateObject(p.TheObject);
                        }
                        catch (Exception ex)
                        {
                            PoolUtils.CheckRethrow(ex);
                        }
                        if (!validate)
                        {
                            try
                            {
                                Destroy(p);
                            }
                            catch
                            {
                                // Ignore - validation failure is more important
                            }
                            p = null;
                            if (create)
                            {
                                NoSuchElementException nsee = new NoSuchElementException(
                                    "Unable to validate object", validationThrowable);
                                throw nsee;
                            }
                        }
                    }
                }
            }

            return(p.TheObject);
        }