Luke Vo
Luke Vo

Reputation: 20668

Ensure concurrent (money) transactions in Entity Framework?

Assume I have an account_profile table, which has Score field that is similar to an account's money (the database type is BIGINT(20) and the EntityFramework type is long, because I don't need decimal). Now I have the following function:

public long ChangeScoreAmount(int userID, long amount)
{
    var profile = this.Entities.account_profile.First(q => q.AccountID == userID);

    profile.Score += amount;

    this.Entities.SaveChanges();

    return profile.Score;
}

However, I afraid that when ChangeScoreAmount are called multiple times concurrently, the final amount won't be correct.

Here are my current solutions I am thinking of:

The problem is, I have never tried a lock on static variable, so I don't know if it is really safe and if any deadlock would occur. Moreover, it may be bad if somewhere else outside this function, a change to Score field is applied midway.

OK this is no longer an option, because my server application will be run on multiple sites, that means the locking variable cannot be used

I am using MySQL Community 5.6.24 and MySQL .NET Connector 6.9.6 in case it matters.

NOTE My server application may be runned on multiple server machines.

Upvotes: 1

Views: 947

Answers (1)

Oleg
Oleg

Reputation: 1458

You can use sql transactions with repeatable read isolation level instead of locking on the application. For example you can write

public long ChangeScoreAmount(int userID, long amount)
{
    using(var ts = new TransactionScope(TransactionScopeOption.RequiresNew, 
        new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead })
    {
      var profile = this.Entities.account_profile.First(q => q.AccountID == userID);

      profile.Score += amount;

      this.Entities.SaveChanges();

      ts.Complete();

      return profile.Score;
    }    
}

Transaction garantees that accountprofile record will not changed in db while you aren't commit or rollback.

Upvotes: 1

Related Questions