none
Using TimeStamp for detect concurreny RRS feed

  • Question

  • Hello ,

      Please Follow me the following steps :

    •   I create the following Table In SQL Server:

             create table Person
              (
                 id int identity primary key,
                 firstName nvarchar(30),
                 lastName nvarchar(30),
                 timestamp

            )

    • I insert the  following data in the table :

             insert person(firstname,lastname) values('first1', 'last1')
             insert person(firstname,lastname) values('first2', 'last2')
             insert person(firstname,lastname) values('first3', 'last3')

    • I used LINQ TO SQL and here it is the following Code  :

            using (TestDataContext ctx = new TestDataContext())
                {
                    var people = ctx.Persons.ToList();

                    Console.WriteLine("Enter a new surname");
                    
                    string surname = Console.ReadLine();

                    // Cause some changes.
                    foreach (Person p in people)
                    {
                        p.lastName = surname;
                    }

                    // HERE...
                    Console.WriteLine("Now's the time to cause a concurrency violation");
                    Console.ReadLine();

                    bool allSaved = false;

                    while (!allSaved)
                    {
                        try
                        {
                           ctx.SubmitChanges(ConflictMode.ContinueOnConflict);

                            allSaved = true;
                        }

                        catch (ChangeConflictException ex)
                        {
                            Console.WriteLine("Hit a conflict - fixing it!");

                            foreach (ObjectChangeConflict conflict in ctx.ChangeConflicts)
                            {
                                conflict.Resolve(RefreshMode.OverwriteCurrentValues);
                            }
                        }

                        catch (Exception ex)
                        {
                            Console.WriteLine("Hit an unexpected exception [{0}]", ex.Message);
                        }
                    }
                }

    •      When I Run the code  :

                  - It asks me to enter sureName .  I enter a name and press enter .

                  - Then it says "Now's the time to cause a concurrency violation"  .  And it is wating .

                  - I Update the table in SQL Server :

                          update person set lastName='anything' where id=2
                          update person set lastName='else' where id=3

                  - I return to the running code . And Press Enter .

                  -  when "SubmitChanges" executes . there is an Exception happened to go to the first catch block .

                  - in the second iteration . When "SubmitChanges" , it suceed .

    •      I think the second time to execute "SubmitChanges" , it must throw exception and sayed "Value of member 'timestamp' of an object of type 'Person' changed."    . Why that is not happened ?


    •    Here it is the attributes of the timeStampColumn :

                     [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_timestamp", AutoSync=AutoSync.Always,  DbType="rowversion NOT NULL", CanBeNull=false, IsDbGenerated=true, IsVersion=true)]
            public System.Data.Linq.Binary timestamp

                  Thanks


               


    ASk

    Sunday, February 26, 2012 9:15 PM

Answers

  • Hi himo,

    Welcome to MSDN Forum.

    After the concurrency occurs, you have called ObjectChangeConflict.Resolve method to resolve it. RefreshMode.OverwriteCurrentValues will forces the Refresh method to override all the current values with the values from the database. After that, the concurrency has already solved, so the code in the try block works without exception.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 28, 2012 3:06 AM
    Moderator