Esempio n. 1
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);
                    }
                }
            }
        }