Пример #1
0
        protected virtual IEnumerable <T> ExecuteQuery <T>(Func <IDbStatement, IEnumerable <T> > query)
        {
            ThrowWhenDisposed();

            TransactionScope scope       = OpenQueryScope();
            IDbConnection    connection  = null;
            IDbTransaction   transaction = null;
            IDbStatement     statement   = null;

            try
            {
                connection         = _connectionFactory.Open();
                transaction        = _dialect.OpenTransaction(connection);
                statement          = _dialect.BuildStatement(scope, connection, transaction);
                statement.PageSize = _pageSize;

                Logger.LogTrace(Messages.ExecutingQuery);
                return(query(statement));
            }
            catch (Exception e)
            {
                statement?.Dispose();
                transaction?.Dispose();
                connection?.Dispose();
                scope?.Dispose();

                Logger.LogDebug(Messages.StorageThrewException, e.GetType());
                if (e is StorageUnavailableException)
                {
                    throw;
                }

                throw new StorageException(e.Message, e);
            }
        }
Пример #2
0
        protected virtual IAsyncEnumerable <T> ExecuteQuery2 <T>(Func <IDbStatement, Task <IAsyncEnumerable <T> > > query)
        {
            return(AsyncEnumerable.Create <T>(async producer =>
            {
                ThrowWhenDisposed();

                TransactionScope scope = OpenQueryScope();
                IDbAsyncConnection connection = null;
                IDbTransaction transaction = null;
                IDbStatement statement = null;
                try
                {
                    connection = await _connectionFactory.OpenAsync();
                    transaction = _dialect.OpenTransaction(connection);
                    statement = _dialect.BuildStatement(scope, connection, transaction);
                    statement.PageSize = _pageSize;

                    Logger.Verbose(Messages.ExecutingQuery);

                    await producer.Yield(await query(statement));

                    Logger.Verbose(Messages.ExecutingQuery);
                }
                catch (Exception e)
                {
                    if (statement != null)
                    {
                        statement.Dispose();
                    }
                    if (transaction != null)
                    {
                        transaction.Dispose();
                    }
                    if (connection != null)
                    {
                        connection.Dispose();
                    }
                    if (scope != null)
                    {
                        scope.Dispose();
                    }

                    Logger.Debug(Messages.StorageThrewException, e.GetType());
                    if (e is StorageUnavailableException)
                    {
                        throw;
                    }

                    throw new StorageException(e.Message, e);
                }
            }));
        }
Пример #3
0
        protected virtual IEnumerable <T> ExecuteQuery <T>(string streamId, Func <IDbStatement, IEnumerable <T> > query)
        {
            ThrowWhenDisposed();

            streamId = streamId.ToHash();
            TransactionScope scope       = OpenQueryScope();
            IDbConnection    connection  = null;
            IDbTransaction   transaction = null;
            IDbStatement     statement   = null;

            try
            {
                connection         = _connectionFactory.OpenReplica(streamId);
                transaction        = _dialect.OpenTransaction(connection);
                statement          = _dialect.BuildStatement(scope, connection, transaction);
                statement.PageSize = _pageSize;

                Logger.Verbose(Messages.ExecutingQuery);
                return(query(statement));
            }
            catch (Exception e)
            {
                if (statement != null)
                {
                    statement.Dispose();
                }
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                if (connection != null)
                {
                    connection.Dispose();
                }
                if (scope != null)
                {
                    scope.Dispose();
                }

                Logger.Debug(Messages.StorageThrewException, e.GetType());
                if (e is StorageUnavailableException)
                {
                    throw;
                }

                throw new StorageException(e.Message, e);
            }
        }
        protected virtual T ExecuteQuery <T>(Guid streamId, Func <IDbStatement, T> query)
        {
            var            scope       = this.OpenQueryScope();
            IDbConnection  connection  = null;
            IDbTransaction transaction = null;
            IDbStatement   statement   = null;

            try
            {
                connection  = this.ConnectionFactory.OpenSlave(streamId);
                transaction = this.Dialect.OpenTransaction(connection);
                statement   = this.Dialect.BuildStatement(connection, transaction, scope);
                return(query(statement));
            }
            catch (Exception e)
            {
                if (statement != null)
                {
                    statement.Dispose();
                }
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                if (connection != null)
                {
                    connection.Dispose();
                }
                scope.Dispose();

                if (e is StorageUnavailableException)
                {
                    throw;
                }

                throw new StorageException(e.Message, e);
            }
        }
 public void Dispose()
 {
     _innerStatement.Dispose();
 }