示例#1
0
        private ObjectDeque <T> Register(K key)
        {
            ObjectDeque <T> objectDeque = null;

            try
            {
                keyLock.EnterReadLock();
                poolMap.TryGetValue(key, out objectDeque);
                if (objectDeque == null)
                {
                    keyLock.ExitReadLock();
                    // Upgrade to write lock
                    keyLock.EnterWriteLock();
                    objectDeque = new ObjectDeque <T>(true);
                    objectDeque.InCrementAndGet();
                    poolMap.TryAdd(key, objectDeque);
                    poolKeyList.Add(key);
                }
                else
                {
                    objectDeque.InCrementAndGet();
                }
            }
            finally
            {
                keyLock.ExitWriteLock();
            }
            return(objectDeque);
        }
示例#2
0
        /// <summary>
        /// Destroy the wrapped, pooled object.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="toDestroy"></param>
        /// <param name="always"></param>
        private bool Destroy(K key, IPooledObject <T> toDestroy, bool always)
        {
            ObjectDeque <T> objectDeque = Register(key);

            try
            {
                bool isIdle = objectDeque.GetIdleObjects().Remove(toDestroy);

                if (isIdle || always)
                {
                    objectDeque.GetAllObjects().TryRemove(new IdentityWrapper <T>(toDestroy.GetObject()), out toDestroy);
                    toDestroy.Invalidate();

                    try
                    {
                        factory.DestroyObject(key, toDestroy);
                    }
                    finally
                    {
                        createdCount--;
                        destroyedCount++;
                        numTotal--;
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            finally
            {
                Deregister(key);
            }
        }
示例#3
0
        private T BorrowObject(K key, long borrowMaxWaitMillis)
        {
            AssertOpen();
            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();
            ObjectDeque <T> objectDeque = Register(key);

            try
            {
                while (p == null)
                {
                    create = false;
                    if (blockWhenExhausted)
                    {
                        p = objectDeque.GetIdleObjects().RemoveFront();
                        if (p == null)
                        {
                            p = Create(key);
                            if (p != null)
                            {
                                create = true;
                            }
                        }
                        if (p == null)
                        {
                            if (borrowMaxWaitMillis < 0)
                            {
                                p = objectDeque.GetIdleObjects().TakeFront();
                            }
                            else
                            {
                                p = objectDeque.GetIdleObjects().RemoveFront();
                            }
                        }
                        if (p == null)
                        {
                            throw new Exception(
                                      "Timeout waiting for idle object");
                        }
                        if (!p.Allocate())
                        {
                            p = null;
                        }
                    }
                    else
                    {
                        p = objectDeque.GetIdleObjects().RemoveFront();
                        if (p == null)
                        {
                            p = Create(key);
                            if (p != null)
                            {
                                create = true;
                            }
                        }
                        if (p == null)
                        {
                            throw new Exception("Pool exhausted");
                        }
                        if (!p.Allocate())
                        {
                            p = null;
                        }
                    }
                    if (p != null)
                    {
                        try
                        {
                            factory.ActivateObject(key, p);
                        }
                        catch (Exception e)
                        {
                            try
                            {
                                Destroy(key, p, true);
                            }
                            catch (Exception)
                            {
                                // ignore
                            }
                            p = null;
                            if (create)
                            {
                                throw new Exception("Unable to activate object");
                            }
                        }
                        if (p != null && (GetTestOnBorrow() || create && getTestOnCreate()))
                        {
                            bool validate = false;
                            try
                            {
                                validate = factory.ValidateObject(key, p);
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                            if (!validate)
                            {
                                try
                                {
                                    Destroy(key, p, true);
                                    destroyedByBorrowValidationCount++;
                                }
                                catch (Exception)
                                {
                                    // Ignore - validation failure is more important
                                }
                                p = null;
                                if (create)
                                {
                                    throw new Exception("Unable to validate object");
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                Deregister(key);
            }
            UpdateStatsBorrow(p, DateTime.Now.CurrentTimeMillis() - waitTime);
            return(p.GetObject());
        }