public void AddToTransactionLog(TransactionLogItem item) { lock (TransactionLog) { TransactionLog.Add(item); } }
//this method uses a global lock // Creates a transaction (initial outcome state = false) then locks the bank to perform a transfer (accounts are not locked) public bool PerformTransactionBankLock(Account sourceAccount, Account destinationAccount, int sum) { bool transferFinished = false; /*if (TransactionIdCounter % 100000 == 0) * { * lock (this) * { * PerformeChecks(); * } * }*/ var transaction = new TransactionLogItem( TransactionIdCounter, sourceAccount.AccountId, destinationAccount.AccountId, sum, false); if (sourceAccount.AccountId == destinationAccount.AccountId) { throw new TransactionException("Cannot perform a transfer into the same account."); } if (sum < 0) { throw new TransactionException("Transaction sum is negative."); } lock (this) { transferFinished = TransferFundsSimple(sourceAccount, destinationAccount, sum); } if (transferFinished == false) { this.AddToAllLogs(transaction, this, sourceAccount, destinationAccount); lock (this) { TransactionIdCounter++; } return(false); } else { transaction.Outcome = true; this.AddToAllLogs(transaction, this, sourceAccount, destinationAccount); lock (this) { TransactionIdCounter++; } return(true); } }
public void GiveMoney(Account destinationAccount, int sum) { destinationAccount.Balance += sum; // logging the transaction : sourceId = 0 -> money from the bank var transaction = new TransactionLogItem( TransactionIdCounter, 0, destinationAccount.AccountId, sum, true); this.AddToAllLogs(transaction, this, destinationAccount); TransactionIdCounter++; }
private void AddToAllLogs(TransactionLogItem transaction, params Loggable[] logs) { foreach (var log in logs) { log.AddToTransactionLog(new TransactionLogItem( transaction.TransactionId, transaction.SourceAccountId, transaction.DestinationAccountId, transaction.TransactionSum, transaction.Outcome)); } }