Ejemplo n.º 1
0
        /// <summary>
        /// Recover abandoned objects which have been checked out but
        /// not used since longer than the removeAbandonedTimeout.
        /// </summary>
        /// <param name="ac"></param>
        private void RemoveAbandoned(AbandonedConfig ac)
        {
            long now     = DateTime.Now.CurrentTimeMillis();
            long timeout = now - ac.getRemoveAbandonedTimeout() * 1000L;
            List <IPooledObject <T> >        remove = new List <IPooledObject <T> >();
            IEnumerator <IPooledObject <T> > it     = _allObjects.Values.GetEnumerator();

            do
            {
                IPooledObject <T> pooledObject = it.Current;
                lock (pooledObject)
                {
                    if (pooledObject.GetState() == PooledObjectState.Allocated &&
                        pooledObject.GetLastUsedTime() <= timeout)
                    {
                        pooledObject.MarkAbandoned();
                        remove.Add(pooledObject);
                    }
                }
            } while (it.MoveNext());

            // Now remove the abandoned objects
            IEnumerator <IPooledObject <T> > itr = remove.GetEnumerator();

            do
            {
                IPooledObject <T> pooledObject = itr.Current;
                try
                {
                    InvalidateObject(pooledObject.GetObject());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            } while (itr.MoveNext());
        }
Ejemplo n.º 2
0
        public T BorrowObject(long borrowMaxWaitMillis)
        {
            AssertOpen();

            AbandonedConfig ac = this.abandonedConfig;

            if (ac != null && ac.getRemoveAbandonedOnBorrow() &&
                (GetNumIdle() < 2) &&
                (GetNumActive() > GetMaxTotal() - 3))
            {
                RemoveAbandoned(ac);
            }

            IPooledObject <T> p = null;

            // Get local copy of current config so it is consistent for entire
            // method execution
            bool blockWhenExhausted = getBlockWhenExhausted();

            bool create;
            long waitTime = DateTime.Now.CurrentTimeMillis();

            while (p == null)
            {
                create = false;
                if (blockWhenExhausted)
                {
                    p = idleObjects.RemoveFront();
                    if (p == null)
                    {
                        p = Create();
                        if (p != null)
                        {
                            create = true;
                        }
                    }
                    if (p == null)
                    {
                        if (borrowMaxWaitMillis < 0)
                        {
                            p = idleObjects.First();
                        }
                        else
                        {
                            p = idleObjects.RemoveFront();
                        }
                    }
                    if (p == null)
                    {
                        throw new Exception(
                                  "Timeout waiting for idle object");
                    }
                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }
                else
                {
                    p = idleObjects.RemoveFront();
                    if (p == null)
                    {
                        p = Create();
                        if (p != null)
                        {
                            create = true;
                        }
                    }
                    if (p == null)
                    {
                        throw new Exception("Pool exhausted");
                    }
                    if (!p.Allocate())
                    {
                        p = null;
                    }
                }

                if (p != null)
                {
                    try {
                        factory.ActivateObject(p);
                    } catch (Exception e) {
                        try {
                            Destroy(p);
                        } catch (Exception e1) {
                            // Ignore - activation failure is more important
                        }
                        p = null;
                        if (create)
                        {
                            throw new Exception("Unable to activate object");
                        }
                    }
                    if (p != null && (GetTestOnBorrow() || create && getTestOnCreate()))
                    {
                        bool validate = false;
                        // Throwable validationThrowable = null;
                        try {
                            validate = factory.ValidateObject(p);
                        } catch (Exception t)
                        {
                            throw t;
                            //   validationThrowable = t;
                        }
                        if (!validate)
                        {
                            try {
                                Destroy(p);
                            } catch (Exception e) {
                                // Ignore - validation failure is more important
                            }
                            p = null;
                            if (create)
                            {
                                throw new Exception("Unable to activate object");
                            }
                        }
                    }
                }
            }

            UpdateStatsBorrow(p, DateTime.Now.CurrentTimeMillis() - waitTime);

            return(p.GetObject());
        }