0

I have created a web application in .NET framework which uses entity framework as ORM. I have a entity as Accounts which contain account details such as ID,Balance and I have implemented Optimistic Concurrency(Row version). Now I am updating the balance from concurrent hits using JMeter say more than 100 users. But I am getting some false values in database.

    public void Credit(int id, int amount)
    {

        bool retry = true;
        while (retry)
            try
            {
                CreditLogic(accountEntity, id, amount);
                retry = false;
            }
            catch (OptimisticConcurrencyException e)
            {

            }
    }

    private void CreditLogic(AccountEntity accountEntity, int id, int amount)
    {

        var details = accountEntity.Accounts.Find(id);
        details.Balance = details.Balance + amount;
        accountEntity.SaveChanges();
    }

public class Account
    {
        public int id { get; set; }
        public int Balance { get; set; }
        [Timestamp]
        public byte[] RowVersion { get; set; }
    }
 public class AccountEntity:DbContext
    {
        public AccountEntity():base("Accounts")
        {

        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Account>().Property(s => s.Balance).IsConcurrencyToken();
            base.OnModelCreating(modelBuilder);
        }

        public virtual DbSet<Account> Accounts { get; set; }
    }

For Example if I am sending 100 users at a time with credit request of 10 and the initial balance was 1200 so updated balance should be 2200 but I am not getting that.

  • [Maybe a duplicate of this](https://stackoverflow.com/questions/17971220/atomic-increment-with-entity-framework), but this question is fairly old so things could have changed. – ProgrammingLlama Sep 09 '19 at 06:07
  • I am getting some false values and seems that some updates are getting failed. – Gaurav Mourya Sep 09 '19 at 06:58
  • Show your `AccountEntity` source code. Do you use `[ConcurrencyCheck]` attribute? Also check this one https://learn.microsoft.com/en-us/ef/ef6/saving/concurrency – mtkachenko Sep 09 '19 at 09:36
  • @mjwills Exact final balance keeps on varying arround 1800 to 2100. – Gaurav Mourya Sep 10 '19 at 04:25
  • @mtkachenko AccountEntity - public class AccountEntity:DbContext { public AccountEntity():base("Accounts") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity().Property(s => s.Balance).IsConcurrencyToken(); base.OnModelCreating(modelBuilder); } public virtual DbSet Accounts { get; set; } } – Gaurav Mourya Sep 10 '19 at 04:25
  • 1
    When you ran a SQL Trace, what exact SQL was being submitted to the database? – mjwills Sep 10 '19 at 04:45
  • 1
    `public class AccountEntity:` Put that in the question. – mjwills Sep 10 '19 at 04:46
  • `I am getting some false values` What do you mean by `false` values? – mjwills Sep 10 '19 at 04:46
  • Do you share instance of `AccountEntity` between all threads? Show full code. – mtkachenko Sep 10 '19 at 11:51

0 Answers0