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.