public void Transfer() { object _lock1, _lock2; if (FromAccount.Id < ToAccount.Id) { _lock1 = FromAccount; _lock2 = ToAccount; } else { _lock1 = ToAccount; _lock2 = FromAccount; } Console.WriteLine($"{Thread.CurrentThread.Name} trying to acquire lock on {((Account)_lock1).Id.ToString()}"); lock (_lock1) { Console.WriteLine($"{Thread.CurrentThread.Name} acquired lock on {((Account)_lock1).Id.ToString()}"); Console.WriteLine($"{Thread.CurrentThread.Name} suspende for 1 sec"); Thread.Sleep(1000); Console.WriteLine($"{Thread.CurrentThread.Name} back on action and trying to acquire lock on {((Account)_lock2).Id.ToString()}"); lock (_lock2) { Console.WriteLine($"{Thread.CurrentThread.Name} acquired lock on {((Account)_lock2).Id.ToString()}"); FromAccount.Withdraw(AmountToTransfer); ToAccount.Deposit(AmountToTransfer); Console.WriteLine($"{Thread.CurrentThread.Name} Transfered {AmountToTransfer} From {FromAccount.Id} to {ToAccount.Id}"); } } }
//------------------------------------------------------------------------------------------- private void UpdatePage() { using (WeavverEntityContainer data = new WeavverEntityContainer()) { //Guid fromGuid = new Guid(FromAccount.SelectedValue); var fAccounts = (from orgs in data.Logistics_Organizations where orgs.OrganizationId == SelectedOrganization.Id orderby orgs.Name select orgs); FromAccount.DataSource = fAccounts; FromAccount.DataTextField = "Name"; FromAccount.DataValueField = "Id"; FromAccount.DataBind(); var cAccounts = (from accounts in data.Accounting_Accounts where accounts.OrganizationId == SelectedOrganization.Id select accounts); ToAccount.DataSource = cAccounts; ToAccount.DataTextField = "Name"; ToAccount.DataValueField = "Id"; ToAccount.DataBind(); } }
public override bool Equals(object obj) { var o = obj as TransferRecordModel; if (o == null) { return(false); } return(FromAccount.Equals(o.FromAccount) && ToAccount.Equals(o.ToAccount) && Amount.Equals(o.Amount) && TransferId.Equals(o.TransferId) && Symbol.Equals(o.Symbol) && Contract.Equals(o.Contract) && CreateTime.Equals(o.CreateTime)); }
public void Transfer(decimal amount) { Console.WriteLine($"Initializing transfer #{FromAccount.Id}:{FromAccount.Balance} -{amount}-> #{ToAccount.Id}:{ToAccount.Balance}"); using (var transaction = Services.AccountRepository.BeginTransaction()) { FromAccount = FromAccount.Withdraw(amount, Services.AccountRepository.Persist); ToAccount = ToAccount.Deposit(amount, Services.AccountRepository.Persist); transaction.Commit(); Console.WriteLine("End of transaction."); } }
public TransactionResult Execute() { TransactionResult result = FromAccount.Withdraw(this); if (result == TransactionResult.SUCCESS) { return(ToAccount.Deposit(this)); } else { return(result); } }
//------------------------------------------------------------------------------------------- protected void Page_Load(object sender, EventArgs e) { Visible = false; return; if (!Roles.IsUserInRole("Administrators") || !Roles.IsUserInRole("Accountants")) { Visible = false; return; } if (!IsPostBack) { PostAt.Text = DateTime.Now.ToString("MM/dd/yy"); using (WeavverEntityContainer data = new WeavverEntityContainer()) { //Guid fromGuid = new Guid(FromAccount.SelectedValue); var fAccounts = (from orgs in data.Accounting_Accounts where orgs.OrganizationId == BasePage.SelectedOrganization.Id orderby orgs.Name select orgs); FromAccount.DataSource = fAccounts; FromAccount.DataTextField = "Name"; FromAccount.DataValueField = "Id"; FromAccount.DataBind(); var cAccounts = (from accounts in data.Accounting_Accounts where accounts.OrganizationId == BasePage.SelectedOrganization.Id select accounts); ToAccount.DataSource = cAccounts; ToAccount.DataTextField = "Name"; ToAccount.DataValueField = "Id"; ToAccount.DataBind(); } } }
//Dead lock solution public void Transfer() { object lock1, lock2; if (FromAccount.Id < ToAccount.Id) { lock1 = FromAccount; lock2 = ToAccount; } else { lock1 = ToAccount; lock2 = FromAccount; } lock (lock1) { Thread.Sleep(1000); lock (lock2) { FromAccount.withDraw(amountTransfer); ToAccount.deposit(amountTransfer); WriteLine("Amount " + amountTransfer + " transfered from " + FromAccount.Id.ToString() + " to " + ToAccount.Id.ToString()); } } }
public override int GetHashCode() { return(FromAccount.GetHashCode() + ToAccount.GetHashCode() + Amount.GetHashCode() + Symbol.GetHashCode() + TransferId.GetHashCode()); }