2

I'm having trouble with concurrency checks using EF6 and MySQL.

The problem I'm having is that I get a concurrency exception thrown when I try to save data to the database. If you examine the sql that is output to the console it tries to query the concurrency field from the database using the old value in the where clause. Because this field has been updated by the database.

Environment:

  • Windows 7 64 bit
  • Visual Studio 2013

Nuget packages installed:

  • EF 6.0.1
  • MySql.ConnectorNET.Data 6.8.3.2
  • MySql.ConnectorNET.Entity 6.8.3.2

Demo Database SQL:

DROP DATABASE IF EXISTS `bugreport`;
CREATE DATABASE IF NOT EXISTS `bugreport`;
USE `bugreport`;

DROP TABLE IF EXISTS `test`;
CREATE TABLE IF NOT EXISTS `test` (
  `TestId` int(10) NOT NULL AUTO_INCREMENT,
  `AStringField` varchar(50) DEFAULT NULL,
  `DateModified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`TestId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO `test` (`TestId`, `AStringField`, `DateModified`) VALUES
    (1, 'Initial Value', '2014-07-11 09:15:52');

Demo code:

using System;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace BugReport
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new BugReportModel())
            {
                context.Database.Log = (s => Console.WriteLine(s));

                var firstTest = context.tests.First();
                firstTest.AStringField = "First Value";

                // Exception is thrown when changes are saved.
                context.SaveChanges();              

                Console.ReadLine();
            } 
        }
    }

    public class BugReportModel : DbContext
    {
        public BugReportModel()
            : base("name=Model1")
        {

        }

        public virtual DbSet<test> tests { get; set; }
    }


    [Table("test")]
    public class test
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int TestId { get; set; }

        [StringLength(50)]
        public string AStringField { get; set; }

        [ConcurrencyCheck()]
        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        [Column(TypeName = "timestamp")]
        public System.DateTime DateModified { get; set; }
    }
}

Update: Filed bug with MySql.

9
  • code first ? Why does the Concurrency field change after reading the row ? If the field in EF that is marked Concurrency changes after reading and prior to update, then indeed EF correctly throws Concurrency exception
    – phil soady
    Jul 9, 2014 at 7:35
  • I read the row then in the next line down modify it. Saving changes throws the exception. Modifying a record should update a concurrency field (i.e. rowversion). I was trying to draw attention to the context.SaveChanges() call by wrapping it in the Try-Catch block to show the exception that is thrown.
    – Rossco
    Jul 9, 2014 at 8:01
  • And yes in this case it is Code First.
    – Rossco
    Jul 9, 2014 at 8:02
  • You cant change the concurrency field
    – phil soady
    Jul 9, 2014 at 8:40
  • Thats why I modified 'AStringField'.
    – Rossco
    Jul 10, 2014 at 6:37

3 Answers 3

1

You should be trying to use the DB Timestamp / Rowversion feature. In EF you declare a ByteArray and nominate it as the Concurrency check field. DB sets the value on creation. All subsequent updates can check the value hasnt changed DB updates rowversion as appropriate. This approach works on SQL server. It should behave the same way on MYSql.

    public  abstract class BaseObject  {
    [Key]
    [Required]
    public virtual int Id { set; get; }

    [ConcurrencyCheck()]
    public virtual byte[] RowVersion { get; set; }

    }

or via fluent if you like // Primary Key this.HasKey(t => t.Id);

        // Properties
        //Id is an int allocated by DB , with string keys, no db generation now
        this.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); // default to db generated

        this.Property(t => t.RowVersion)
            .IsRequired()
            .IsFixedLength()
            .HasMaxLength(8)
            .IsRowVersion(); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Docu on the optimistic concurrency pattern

2
  • 2
    MySQL doesn't have an equivalent to SQL Server's rowversion. MS have deprecated 'timestamp' in favour of 'rowversion'.
    – Rossco
    Jul 10, 2014 at 23:54
  • Sadly this won't work for MySQL .NET Connector 6.9.10. I've just submitted a PR that will fix it. May 3, 2018 at 5:26
0

Workarround with Interceptor

I see that MySQL Connector bug pointed in question do not have a fix yet (since 2014) and I wrote that "solution" (I know this is ugly) until the they fix it.

I create an DBCommandInterceptor and override the ReaderExecuting to replace the equal operator (=) in last WHERE to a not equal operator (<>) because the pattern for the update is something like "UPDATE ...; SELECT ... WHERE (row_version_field = @parameter)"

In the code bellow replace row_version in regular expression with the name of your row version field.

public class ConcurrencyFixInterceptor : DbCommandInterceptor
{
    private static Regex concurrencyPattern = 
        new Regex(@"^UPDATE[\S\s]+SELECT[\S\s]+\(.?row_version.?\s(=)\s@[\w\d]+\)$", 
            RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);

    public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
    {
        Match macth = concurrencyPattern.Match(command.CommandText);
        if (macth.Success)
        {
            command.CommandText = 
                command.CommandText.
                Remove(macth.Groups[1].Index, 1).
                Insert(macth.Groups[1].Index, "<>");
        }
        base.ReaderExecuting(command, interceptionContext);
    }

}

I use a row version in MySQL with a TIMESTAMP(5) field type.

0

I have just submitted a PR to MySQL .NET Connector v6.9.10 that provides a workaround solution for this issue.

The workaround avoids use of TIMESTAMP or DATETIME values to perform optimistic locking using a safer BIGINT RowVersion value that is incremented via a BEFORE UPDATE trigger. This fix will now support optimistic locking with an external (non-EF) application. If I can fix a 2nd bug related to TIMESTAMP / DATETIME then ConcurrencyCheck should work with these types as well.

EF6:

public class MyTable
{
  [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
  public virtual int Id { get; set; }

  [Required, MaxLength(45)]
  public virtual string Name { get; set; }

  [ConcurrencyCheck, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
  [Column(TypeName = "bigint")]
  public virtual long RowVersion { get; set; }
}

SQL:

CREATE TABLE IF NOT EXISTS `mytable` (
    Id int(11) NOT NULL,
    Name varchar(45) NOT NULL,
    RowVersion bigint NOT NULL DEFAULT 0,
    PRIMARY KEY (`Id`)
) ENGINE=InnoDB

CREATE TRIGGER `trg_mytable_before_update` 
BEFORE UPDATE ON `mytable`
FOR EACH ROW SET NEW.RowVersion = OLD.RowVersion + 1;

TIMESTAMP Solution?

I'm also investigating how to performing optimistic locking with a TIMESTAMP field.

Firstly, you need to use a more fine grained timestamp value.

So for example if you use the following, your timestamp value will be truncated to the nearest second (not very safe for optimistic locking).

UpdatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIME ON UPDATE CURRENT_TIME

Instead you should use following to record microsecond precision.

UpdatedAt TIMESTAMP(6) NOT NULL DEFAULT NOW(6) ON UPDATE NOW(6)

Secondly, I'm observing a bug that I'm reproducing within the environment of the MySQL .NET Connector unit test suite combined with the PR patch I've just submitted. EF6 now generates the correct optimistic locking SQL to perform an UPDATE followed by the SELECT (now fixed) that returns the updated TIMESTAMP field. However the MySQL connector returns a zero TIMESTAMP (0000-00-00 00:00:00.000000) even though executing the exact same UPDATE and SELECT in MySQL Workbench it returns a valid non-zero TIMESTAMP value. I've observed the packets read via the connection socket return the string '0000-00-00 00:00:00.000000' so its probably related to the MySQL session configuration in some way. Hints welcome! I'm currently testing this with MySQL v5.6.26 (Windows).

Multiple optimistic lock fields

In our case, we have a legacy MS-Access app that uses a TIMESTAMP in most tables to perform optimistic locking. This is a convenient solution for MS-Access as it detects the presence of any TIMESTAMP column and automatically applies optimistic locking to this column when it finds one.

Since we currently don't have optimistic locking working with EF6 for TIMESTAMP columns we've added a second optimistic lock column on each table we care about by creating a BIGINT RowVersion column as that is incremented via a BEFORE INSERT trigger. So now for each UPDATE both the existing TIMESTAMP column and the new RowVersion column are update so either can be used to detect a change. Not ideal but it works!

1
  • Hopefully I'll soon have a PR for EF6 / TIMESTAMP(6) optimistic locking as well. I've not yet checked that TIMESTAMP(6) works with MS-Access. May 3, 2018 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.