Optimistic Offline Locking

Post on 15-Dec-2014

182 views 1 download

description

討論關於Offline Locing的其中一種。Optimistic Offline Locking。Team內部教育訓練用。

Transcript of Optimistic Offline Locking

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock

Aecho

Penpower, Inc

aecho.liu@penpower.com.tw

2013, 08

1 / 29

Overview Concurrency Optimistic Lock Entity Framework

Overview

Overview

Concurrency

Optimistic Lock

Entity Framework

2 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency

What is concurrency ???

.

......Several computations are executing simultaneously.

3 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency

What is concurrency ???

.

......Several computations are executing simultaneously.

4 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency ProblemsWhenever you have multiple processes, or threads manipulatingthe same data, you run into concurrency problems.

.

......Concurrency is hard to think about, and it’s difficult to enumeratepossible scenarios.

.

......Concurrency is hard to test for.

5 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency ProblemsWhenever you have multiple processes, or threads manipulatingthe same data, you run into concurrency problems.

.

......Concurrency is hard to think about, and it’s difficult to enumeratepossible scenarios.

.

......Concurrency is hard to test for.

6 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency ProblemsThe essential concurrency problems.

• Lost Updates• Inconsistent Reads

7 / 29

Overview Concurrency Optimistic Lock Entity Framework

Lost Updates洋蔥.

......

get CustomerA ;

.

......

update CustomerA ;

九層塔

.

......

get CustomerA ;

.

......

update CustomerA ;// 洋蔥 , l o s t update

8 / 29

Overview Concurrency Optimistic Lock Entity Framework

Inconsistent Reads洋蔥.

......

get CustomerA ;

.

......

update CustomerA ;

九層塔

.

......

get CustomerA ;

.

......

// CustomerA , I n c o n s i s t e n t Reads// get CustomerA ;update CustomerA ;

9 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency Controls• Pessimistic Lock• Optimistic Lock

10 / 29

Overview Concurrency Optimistic Lock Entity Framework

Pessimistic Lock

.

......Prevent conflicts between concurrent business transactions byallowing only one business transaction to access data at once.

.

......Conflict Prevention.

.

......Hard to implement.

11 / 29

Overview Concurrency Optimistic Lock Entity Framework

Pessimistic Lock

.

......Prevent conflicts between concurrent business transactions byallowing only one business transaction to access data at once.

.

......Conflict Prevention.

.

......Hard to implement.

12 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock

.

......Prevent conflicts between concurrent business transactions, bydetecting a conflict and rolling back the transaction.

.

......Conflict Detection.

.

......Easy to implement.

13 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock

.

......Prevent conflicts between concurrent business transactions, bydetecting a conflict and rolling back the transaction.

.

......Conflict Detection.

.

......Easy to implement.

14 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock• Most common implementation

Each record associates with a version number.1

.

......It checks the version number before executing update or deletesql command.

• Bad IdeaUse modification time-stamp rather than a version counter.

.

......System clocks are simply too unreliable. Even more so if you arecoordinating across multiple servers.

1row version15 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock• Most common implementation

Each record associates with a version number.1

.

......It checks the version number before executing update or deletesql command.

• Bad IdeaUse modification time-stamp rather than a version counter.

.

......System clocks are simply too unreliable. Even more so if you arecoordinating across multiple servers.

1row version16 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock• Most common implementation

Each record associates with a version number.1

.

......It checks the version number before executing update or deletesql command.

• Bad IdeaUse modification time-stamp rather than a version counter.

.

......System clocks are simply too unreliable. Even more so if you arecoordinating across multiple servers.

1row version17 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock• Most common implementation

Each record associates with a version number.1

.

......It checks the version number before executing update or deletesql command.

• Bad IdeaUse modification time-stamp rather than a version counter.

.

......System clocks are simply too unreliable. Even more so if you arecoordinating across multiple servers.

1row version18 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock洋蔥.

......

get customerA ;

.

......

update customerA ;// ! Detect a c o n f l i c t .// ! Because the customerA has been

updated .

九層塔

.

......

get customerA ;. . .

update customerA ;

How about pessimistic lock ?

19 / 29

Overview Concurrency Optimistic Lock Entity Framework

Optimistic Lock洋蔥.

......

get customerA ;

.

......

update customerA ;// ! Detect a c o n f l i c t .// ! Because the customerA has been

updated .

九層塔

.

......

get customerA ;. . .

update customerA ;

How about pessimistic lock ?

20 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency Problems.

......

Pessimistic OptimisticLost Update No NoInconsistent reads No Maybe

About Optimistic lock,• It cannot deal with inconsistent read problem.

21 / 29

Overview Concurrency Optimistic Lock Entity Framework

Concurrency Problems.

......

Pessimistic OptimisticLost Update No NoInconsistent reads No Maybe

About Optimistic lock,• It cannot deal with inconsistent read problem.

22 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity FrameworkThe MSSQL database server supports the optimistic lockingmechanism.

.

......MSSQL 2012, timestamp column type.

23 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity FrameworkThe MSSQL database server supports the optimistic lockingmechanism.

.

......MSSQL 2012, timestamp column type.

24 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity Framework.Code First..

......

[ Table ( ”db_contact” ) ]pub l i c c l a s s Contact {

[ Key ]pub l i c Guid Guid { get ; s e t ; }

pub l i c DateTime CreatedTimeUTC { get ; s e t ; }pub l i c DateTime ModifiedTimeUTC { get ; s e t ; }

[ Timestamp ]pub l i c byte [ ] db_row_version { get ; s e t ; }

[ NotMapped ]pub l i c s t r i n g RowVersion {

// Convert db_row_version bytes to s t r i n g .get { . . . }// Convert s t r i n g to db_row_version bytes .s e t { . . . }

}}

25 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity Framework.DDL Syntax..

......

CREATE TABLE [ dbo ] . [ ContactSet ] ([ Guid ] u n i q u e i d e n t i f i e r NOT NULL,[ CreatedTimeUTC ] datet ime NOT NULL,[ ExpiredTimeUTC ] datet ime NOT NULL,[ db_row_version ] rowvers ion NOT NULL,

) ;GO

26 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity FrameworkThe timestamp column type in DDL is deprecated..timestamp column type..

......

CREATE TABLE ExampleTable2 ( PriKey i n t PRIMARY KEY, VerCol timestamp ) ;

.rowversion column type..

......

CREATE TABLE ExampleTable2 ( PriKey i n t PRIMARY KEY, VerCol rowvers ion ) ;

.NOTE..

......The column type will be timestamp.

27 / 29

Overview Concurrency Optimistic Lock Entity Framework

Entity FrameworkThe timestamp column type in DDL is deprecated..timestamp column type..

......

CREATE TABLE ExampleTable2 ( PriKey i n t PRIMARY KEY, VerCol timestamp ) ;

.rowversion column type..

......

CREATE TABLE ExampleTable2 ( PriKey i n t PRIMARY KEY, VerCol rowvers ion ) ;

.NOTE..

......The column type will be timestamp.

28 / 29

Overview Concurrency Optimistic Lock Entity Framework

The End

Any questions ?

29 / 29