What happens if 2 or more people update the record at the same time?

I use NHibernate with the version property, which automatically increments every time my common root is updated. What happens if 2 or more people update the same record at the same time?

Also, how would I check this out?

Please note that this is not the situation I was in, just interesting.

+5
source share
5 answers

Which atomic and what not

, SQL Server . NHibernate ( O/RM), , select , , update . not . , , . , , , , .

, - , . , , :

public class BodyOfWater
{
    public virtual int Id { get; set; }
    public virtual StateOfMatter State { get; set; }

    public virtual void Freeze()
    {
        if (State != StateOfMatter.Liquid)
            throw new InvalidOperationException("You cannot freeze a " + State + "!");
        State = StateOfMatter.Solid;
    }

    public virtual void Boil()
    {
        if (State != StateOfMatter.Liquid)
            throw new InvalidOperationException("You cannot boil a " + State + "!");
        State = StateOfMatter.Gas;
    }
}

, :

new BodyOfWater
{
    Id = 1,
    State = StateOfMatter.Liquid
};

, . A :

using (var transaction = sessionA.BeginTransaction())
{
    var water = sessionA.Get<BodyOfWater>(1);
    water.Freeze();
    sessionA.Update(water);

    // Same point in time as the line indicated below...

    transaction.Commit();
}

B ( !)...

using (var transaction = sessionB.BeginTransaction())
{
    var water = sessionB.Get<BodyOfWater>(1);

    // ... Same point in time as the line indicated above.

    water.Boil();
    sessionB.Update(water);
    transaction.Commit();
}

... !!! ? . , : " Solid!"? B , A , , -, , .

, Version NHibernate <version />:

public virtual int Version { get; set; }

, NHibernate , , , , . concurrency -naive sql, ...

update BodyOfWater set State = 'Gas' where Id = 1;

... NHibernate :

update BodyOfWater set State = 'Gas', Version = 2 where Id = 1 and Version = 1;

, , 0, NHibernate , - - - , , - , . NHibernate a StaleObjectStateException.

-

select update, concurrency. "" -. , HTML- . . , , - , , .

, , , , . , , , , , select update, , , "" . "", , , , .

+5

: . . - , , - . , .

+4

, . SQL Server . , . .

+4

, , ( ) SQL Server. ( " " )

Concurrency :

+4

, . , , , , , .

MyTable  
Id | RowVersion | Description  
1  | 1          | this description

SQL:
1-
MyTable = 'test', rowversion = 2 id = 1 rowversion = 1

:

MyTable  
Id | RowVersion | Description  
1  | 2          | test

Second update
Description update MyTable = 'second update', rowversion = 2, where id = 1 and rowversion = 1

nothing updated.

+1
source

All Articles