Exemplo n.º 1
0
        public IConnectionWrapper GetConnection()
        {
            if (_connectionFactory == null)
            {
                throw new ConnectionPoolWrongUseException();
            }
            if (_connectionFreeWrappers.Count > 0)
            {
                var connection = _connectionFreeWrappers.Pop();
                _busyWrappers.Add(connection);
                return(connection);
            }

            if (_connectionFreeWrappers.Count == 0 && _busyWrappers.Count >= Limit)
            {
                throw new OperationCanceledException();
            }

            if (_created == Limit)
            {
                throw new OperationCanceledException();
            }

            _created++;
            var newConnection = new ConnectionWrapper(_connectionFactory.CreateNew(), this, _created);

            _busyWrappers.Add(newConnection);
            return(newConnection);
        }