Esempio n. 1
0
        public override void ReturnObject(T obj)
        {
            PooledObject <T> p = allObjects[obj] as PooledObject <T>;

            if (p == null)
            {
                throw new IllegalStateException("Returned object not currently part of this pool");
            }

            if (TestOnReturn)
            {
                if (!factory.ValidateObject(obj))
                {
                    try
                    {
                        Destroy(p);
                    }
                    catch
                    {
                    }
                    return;
                }
            }

            try
            {
                factory.SuspendObject(obj);
            }
            catch
            {
                try
                {
                    Destroy(p);
                }
                catch
                {
                }

                return;
            }

            if (!p.Deallocate())
            {
                throw new IllegalStateException("Object has already been retured to this pool");
            }

            int maxIdle = MaxIdle;

            if (IsClosed || maxIdle > -1 && maxIdle <= idleObjects.Size())
            {
                try
                {
                    Destroy(p);
                }
                catch
                {
                }
            }
            else
            {
                if (Lifo)
                {
                    idleObjects.AddFirst(p);
                }
                else
                {
                    idleObjects.AddLast(p);
                }
            }
        }
Esempio n. 2
0
        public void Evict()
        {
            CheckClosed();

            if (idleObjects.Size() == 0)
            {
                return;
            }

            PooledObject <T> underTest = null;

            lock (evictionLock)
            {
                bool testWhileIdle     = TestWhileIdle;
                long idleEvictTime     = Int64.MaxValue;
                long idleSoftEvictTime = Int64.MaxValue;

                if (MinEvictableIdleTimeMillis > 0)
                {
                    idleEvictTime = MinEvictableIdleTimeMillis;
                }
                if (SoftMinEvictableIdleTimeMillis > 0)
                {
                    idleSoftEvictTime = SoftMinEvictableIdleTimeMillis;
                }

                for (int i = 0, m = NumTests; i < m; i++)
                {
                    if (evictionIterator == null || !evictionIterator.HasNext)
                    {
                        if (Lifo)
                        {
                            evictionIterator = idleObjects.DescendingIterator();
                        }
                        else
                        {
                            evictionIterator = idleObjects.Iterator();
                        }
                    }
                    if (!evictionIterator.HasNext)
                    {
                        // Pool exhausted, nothing to do here
                        return;
                    }

                    try
                    {
                        underTest = evictionIterator.Next();
                    }
                    catch (NoSuchElementException)
                    {
                        // Object was borrowed in another thread
                        // Don't count this as an eviction test so reduce i;
                        i--;
                        evictionIterator = null;
                        continue;
                    }

                    if (!underTest.StartEvictionTest())
                    {
                        // Object was borrowed in another thread
                        // Don't count this as an eviction test so reduce i;
                        i--;
                        continue;
                    }

                    if (idleEvictTime < underTest.IdleTime.TotalMilliseconds ||
                        (idleSoftEvictTime < underTest.IdleTime.TotalMilliseconds && MinIdle < idleObjects.Size()))
                    {
                        Destroy(underTest);
                    }
                    else
                    {
                        if (testWhileIdle)
                        {
                            bool active = false;
                            try
                            {
                                factory.ActivateObject(underTest.TheObject);
                                active = true;
                            }
                            catch
                            {
                                Destroy(underTest);
                            }

                            if (active)
                            {
                                if (!factory.ValidateObject(underTest.TheObject))
                                {
                                    Destroy(underTest);
                                }
                                else
                                {
                                    try
                                    {
                                        factory.SuspendObject(underTest.TheObject);
                                    }
                                    catch
                                    {
                                        Destroy(underTest);
                                    }
                                }
                            }
                        }
                        underTest.EndEvictionTest(idleObjects);
                    }
                }
            }
        }
Esempio n. 3
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);
        }