async Task IReliableDispatcher <T> .EnqueueAsync(T item)
        {
            using (var transaction = _transactionFactory.Create())
            {
                await _reliableQueue.Value.EnqueueAsync(transaction, item).ConfigureAwait(false);

                await transaction.CommitAsync().ConfigureAwait(false);
            }

            RestartDispatcherIfPaused();
        }
        public IEnumerable <TankHeroAdto> GetAll()
        {
            using (ITransaction transaction = _transactionFactory.Create())
            {
                IEnumerable <Hero> heroes = _heroRepository.GetAll();

                IEnumerable <TankHeroAdto> heroAdtos = ObjectMapper.MapList <Hero, TankHeroAdto>(heroes);

                transaction.Commit();

                return(heroAdtos);
            }
        }
Пример #3
0
        public void store_a_positive_transaction_on_deposit()
        {
            var amountToDepose     = 500;
            var createdTransaction = new Transaction(DateTime.Now, amountToDepose);

            A.CallTo(() => transactionFactory.Create(amountToDepose))
            .Returns(createdTransaction);

            accountService.Deposit(amountToDepose);

            A.CallTo(() => transactionFactory.Create(amountToDepose)).MustHaveHappened(Repeated.Exactly.Once)
            .Then(A.CallTo(() => transactionRepository.Save(createdTransaction)).MustHaveHappened(Repeated.Exactly.Once));
        }
Пример #4
0
 public ITransaction BeginTransaction()
 {
     if (CurrentTransaction == null)
     {
         var tx = _transactionFactory.Create(null);
         _currentTransactionAccessor.CurrentTransaction = tx;
         return(tx);
     }
     else
     {
         var tx = _transactionFactory.Create(_currentTransactionAccessor.CurrentTransaction);
         _currentTransactionAccessor.CurrentTransaction = tx;
         return(tx);
     }
 }
        /// <summary>
        /// Begin a transaction with the specified isolation level and return
        /// the associated <c>ITransaction</c> instance
        /// </summary>
        /// <param name="session"></param>
        /// <param name="isolationLevel"></param>
        /// <returns></returns>
        public ITransaction BeginTransaction(ISession session, IsolationLevel isolationLevel)
        {
            ITransaction transaction = transactionFactory.Create(session);

            transaction.Begin(isolationLevel);
            return(transaction);
        }
        public async Task <Subscription> Add(string firstName, string lastName, string emailAddress, Func <Subscription, Task <Subscription> > continueWith)
        {
            using (var connection = CreateConnection())
            {
                connection.Open();
                using (var transaction = _transactionFactory.Create(connection))
                {
                    try
                    {
                        var result = await connection.QueryAsync <Subscription>(InsertSql, new { id = Guid.NewGuid(), firstName, lastName, emailAddress, subscriptionKey = Guid.NewGuid() }, transaction)
                                     .ConfigureAwait(false);

                        var subscription = result.Single();

                        if (continueWith != null)
                        {
                            subscription = await continueWith(subscription);
                        }

                        transaction.Commit();

                        return(subscription);
                    }
                    catch
                    {
                        transaction.Rollback();
                        return(null);
                    }
                }
            }
        }
        /// <inheritdoc />
        public QueueRemoveResult Handle(DeleteQueueTablesCommand inputCommand)
        {
            using (var connection = _dbConnectionFactory.Create())
            {
                connection.Open();
                using (var trans = _transactionFactory.Create(connection).BeginTransaction())
                {
                    foreach (var table in _tableNameHelper.Tables)
                    {
                        var delete =
                            _tableExists.Handle(
                                new GetTableExistsTransactionQuery(connection, trans, table));

                        if (!delete)
                        {
                            continue;
                        }

                        using (var commandSql = connection.CreateCommand())
                        {
                            commandSql.Transaction = trans;
                            _prepareDeleteTable.Handle(new DeleteTableCommand(table), commandSql,
                                                       CommandStringTypes.DeleteTable);
                            commandSql.ExecuteNonQuery();
                        }
                    }
                    trans.Commit();
                    return(new QueueRemoveResult(QueueRemoveStatus.Success));
                }
            }
        }
Пример #8
0
        private static async Task TransactionAsync(SqlDb dbTx, ITransactionFactory txFactory)
        {
            try
            {
                await txFactory.ExecuteAsync(async() => {
                    await dbTx.DeleteAsync <Order>((o, s) => o.Id > 0);
                    await dbTx.DeleteAsync <Custom>((c, s) => c.Id > 0);

                    throw new Exception("Rollback");
                });
            }
            catch
            {
                //ignore Exception
            }

            var tx = txFactory.Create();//Create(IsolationLevel)
            await dbTx.DeleteAsync <Order>((o, s) => o.Id > 0);

            await dbTx.DeleteAsync <Custom>((c, s) => c.Id > 0);

            //tx.Commit();
            tx.Rollback();

            var count = await dbTx.SelectAsync <Order, int>((o, s) => s.Count(), null);

            Console.WriteLine(count);
        }
Пример #9
0
    private void ShipOrderInTransaction(Order order)
    {
        using (var transaction = _transactionFactory.Create())
        {
            ShipOrderInternal(order);

            transaction.Commit();
        }
    }
Пример #10
0
        public Transaction Process(ITransactionFactory factory, Vault vault, StockPrice stockPrice)
        {
            //No Current Transaction
            if (vault.CurrentTransaction == null)
            {
                var stockBuyCount = (long)Math.Floor(vault.Money / stockPrice.Price);
                var transaction   = factory.Create(TransactionType.Buy, stockBuyCount, stockPrice.Date, stockPrice.Price);

                while (transaction.GetAmount() + ComputeTransactionFee(transaction.GetAmount()) > vault.Money)
                {
                    stockBuyCount--;
                    transaction = factory.Create(TransactionType.Buy, stockBuyCount, stockPrice.Date, stockPrice.Price);
                }

                return(transaction);
            }
            else
            {
                var highThresholdPrice = vault.CurrentTransaction.Price * ((decimal)100 + this.highSellingThreshold) / (decimal)100;
                var lowThresholdPrice  = vault.CurrentTransaction.Price * ((decimal)100 - this.lowSellingThreshold) / (decimal)100;

                var sellingPrice = decimal.MinusOne;

                if (stockPrice.Price > highThresholdPrice)
                {
                    sellingPrice = highThresholdPrice;
                }
                else if (stockPrice.Price < lowThresholdPrice)
                {
                    sellingPrice = lowThresholdPrice;
                }

                if (sellingPrice != decimal.MinusOne)
                {
                    var transaction = factory.Create(TransactionType.Sell, vault.CurrentTransaction.Count,
                                                     stockPrice.Date, sellingPrice);

                    return(transaction);
                }
            }

            return(null);
        }
Пример #11
0
        private void SendMessage(UserMessage userMessage)
        {
            if (_lastTransaction != _lastCommitedTransaction)
            {
                throw new InvalidOperationException("Can't send new transaction before previos was not confirmed");
            }

            Log("Sending new {0}", userMessage);
            var transaction = _transactionFactory.Create(User, _lastTransaction?.Signature ?? HashValue.Empty, userMessage);
            var message     = new NewTransactionMessage(UserNode, transaction);

            _messageBus.Push(message);
        }
Пример #12
0
        public IAccount AddTransaction(int accountId, decimal amount, string reference, string comment)
        {
            var transaction = _transactionFactory.Create(accountId, amount, reference, comment);

            _transactionRepo.Add(transaction, true);

            var account = _accoutRepo.Find(accountId);

            account.Balance = GetAccountBalance(accountId);

            _accoutRepo.Update(account, true);

            return(account);
        }
        /// <inheritdoc />
        public QueueStatuses Handle(DoesJobExistQuery <TConnection, TTransaction> query)
        {
            if (query.Connection != null)
            {
                return(RunQuery(query, query.Connection, query.Transaction));
            }

            using (var connection = _dbConnectionFactory.Create())
            {
                connection.Open();
                using (var trans = _transactionFactory.Create(connection).BeginTransaction())
                {
                    return(RunQuery(query, connection, trans));
                }
            }
        }
Пример #14
0
 /// <inheritdoc />
 public QueueCreationResult Handle(CreateJobTablesCommand <ITable> command)
 {
     using (var conn = _dbConnectionFactory.Create())
     {
         conn.Open();
         using (var trans = _transactionFactory.Create(conn).BeginTransaction())
         {
             using (var commandSql = conn.CreateCommand())
             {
                 commandSql.Transaction = trans;
                 _prepareCommandHandler.Handle(command, commandSql, CommandStringTypes.CreateJobTables);
                 commandSql.ExecuteNonQuery();
             }
             trans.Commit();
         }
     }
     return(new QueueCreationResult(QueueCreationStatus.Success));
 }
Пример #15
0
        public bool DoWithdrawal(Account account, int amount)
        {
            var transactionBancaire = transactionFactory.Create(account, amount);

            if (transactionBancaire.Validate())
            {
                transactionBancaire.Process();

                try
                {
                    cashDispenser.Dispense(amount);
                    return(true);
                }
                catch (OutOfMoneyException e)
                {
                    transactionBancaire.Rollback();
                }
            }
            return(false);
        }
Пример #16
0
        /// <inheritdoc />
        public long Handle(DeleteMessageCommand <long> command)
        {
            using (var connection = _dbConnectionFactory.Create())
            {
                connection.Open();
                using (var trans = _transactionFactory.Create(connection).BeginTransaction())
                {
                    using (var commandSql = connection.CreateCommand())
                    {
                        commandSql.Transaction = trans;

                        //delete the meta data record
                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.DeleteFromMetaData);
                        commandSql.ExecuteNonQuery();

                        //delete the message body
                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.DeleteFromQueue);
                        commandSql.ExecuteNonQuery();

                        //delete any error tracking information
                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.DeleteFromErrorTracking);
                        commandSql.ExecuteNonQuery();

                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.DeleteFromMetaDataErrors);
                        commandSql.ExecuteNonQuery();

                        //delete status record
                        if (!_options.Value.EnableStatusTable)
                        {
                            trans.Commit();
                            return(1);
                        }

                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.DeleteFromStatus);
                        commandSql.ExecuteNonQuery();
                        trans.Commit();
                        return(1);
                    }
                }
            }
        }
        public QueueCreationResult Handle(CreateQueueTablesAndSaveConfigurationCommand <ITable> command)
        {
            using (var conn = _connectionFactory.Create())
            {
                conn.Open();
                using (var trans = _transactionFactory.Create(conn).BeginTransaction())
                {
                    using (var commandSql = conn.CreateCommand())
                    {
                        commandSql.Transaction = trans;
                        _prepareCommand.Handle(command, commandSql, CommandStringTypes.CreateQueueTables);
                        commandSql.ExecuteNonQuery();
                    }

                    //save the configuration
                    SaveConfiguration(conn, trans);
                    trans.Commit();
                }
            }
            return(new QueueCreationResult(QueueCreationStatus.Success));
        }
 public long Handle(ResetHeartBeatCommand <long> inputCommand)
 {
     using (var connection = _connectionFactory.Create())
     {
         connection.Open();
         using (var trans = _transactionFactory.Create(connection).BeginTransaction())
         {
             using (var command = connection.CreateCommand())
             {
                 command.Transaction = trans;
                 _prepareCommand.Handle(inputCommand, command, CommandStringTypes.ResetHeartbeat);
                 var result = command.ExecuteNonQuery();
                 if (result > 0)
                 {
                     trans.Commit();
                 }
                 return(result);
             }
         }
     }
 }
        /// <inheritdoc />
        public void Handle(MoveRecordToErrorQueueCommand <long> command)
        {
            if (_options.Value.EnableHoldTransactionUntilMessageCommitted)
            {
                HandleForTransaction(command);
            }
            else
            {
                using (var conn = _dbConnectionFactory.Create())
                {
                    conn.Open();
                    using (var trans = _transactionFactory.Create(conn).BeginTransaction())
                    {
                        using (var commandSql = conn.CreateCommand())
                        {
                            commandSql.Transaction = trans;
                            _prepareCommand.Handle(command, commandSql, CommandStringTypes.MoveToErrorQueue);
                            var iCount = commandSql.ExecuteNonQuery();
                            if (iCount != 1)
                            {
                                return;
                            }

                            //the record is now in the error queue, remove it from the main queue
                            _deleteMetaCommandHandler.Handle(new DeleteMetaDataCommand(command.QueueId, conn, trans));

                            if (!_options.Value.EnableStatusTable)
                            {
                                trans.Commit();
                                return;
                            }

                            //update the status record
                            _setStatusCommandHandler.Handle(new SetStatusTableStatusTransactionCommand(command.QueueId, conn, QueueStatuses.Error, trans));
                        }
                        trans.Commit();
                    }
                }
            }
        }
Пример #20
0
        public void Deposit(int amount)
        {
            var transaction = _transactionFactory.Create(amount);

            _transactionRepository.Save(transaction);
        }