Example #1
0
        public SimpleDbConnectionPool(Options options, DatabaseOption databaseOption)
        {
            DatabaseOption = databaseOption;
            int maximumRetained = Math.Max(options.MaximumRetained, 0);

            if (maximumRetained > 0)
            {
                _pooledObjectPolicy = new PooledObjectPolicy(maximumRetained, CreateDbConnection);
                ConnectionPool      = new DefaultObjectPool <DbConnection>(_pooledObjectPolicy, maximumRetained);
            }
        }
        public SimpleDbConnectionPool(Options options, Func <DbConnection> funcCreateConnection)
        {
            _funcCreateConnection = funcCreateConnection;
            int maximumRetained = Math.Max(options.MaximumRetained, 0);

            if (maximumRetained > 0)
            {
                _pooledObjectPolicy = new PooledObjectPolicy(maximumRetained, _funcCreateConnection);
                _inner = new DefaultObjectPool <DbConnection>(_pooledObjectPolicy, maximumRetained);
            }
        }
Example #3
0
    /// <summary>
    /// Creates an instance of <see cref="DefaultObjectPool{T}"/>.
    /// </summary>
    /// <param name="policy">The pooling policy to use.</param>
    /// <param name="maximumRetained">The maximum number of objects to retain in the pool.</param>
    public DefaultObjectPool(IPooledObjectPolicy <T> policy, int maximumRetained)
    {
        _policy          = policy ?? throw new ArgumentNullException(nameof(policy));
        _fastPolicy      = policy as PooledObjectPolicy <T>;
        _isDefaultPolicy = IsDefaultPolicy();

        // -1 due to _firstItem
        _items = new ObjectWrapper[maximumRetained - 1];

        bool IsDefaultPolicy()
        {
            var type = policy.GetType();

            return(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DefaultPooledObjectPolicy <>));
        }
    }
        /// <summary>
        /// Creates an instance of <see cref="Microsoft.Extensions.ObjectPool.DefaultObjectPool{T}"/>.
        /// </summary>
        /// <param name="policy">The pooling policy to use.</param>
        /// <param name="poolSize">The maximum number of objects to retain in the pool.</param>
        public DefaultObjectPool(IPooledObjectPolicy <T> policy, int poolSize)
        {
            _policy          = policy ?? throw new ArgumentNullException(nameof(policy));
            PoolSize         = poolSize;
            _fastPolicy      = policy as PooledObjectPolicy <T>;
            _isDefaultPolicy = IsDefaultPolicy();

            _items = Enumerable.Range(0, poolSize).Select(i => new ObjectWrapper <T>()
            {
                Element = _fastPolicy.OptimisticObjectCreation ? Create() : null, Index = i                                                                        /*, CheckOut = DateTime.MinValue*/
            }).ToArray();

            bool IsDefaultPolicy()
            {
                var type = policy.GetType();

                return(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(DefaultPooledObjectPolicy <>));
            }
        }