Database Access Synchronization in a Distributed Application

The common bit of programming logic that I find in myself is often something like the following pseudocode:

Let X = some value
Let Database = some external Database handle

if !Database.contains(X):
   SomeCalculation()
   Database.insert(X)

However, in a multi-threaded program, we have a race condition. Thread A can check if it is Xin Database, find it, and then go to the call SomeCalculation(). Meanwhile, Thread B also checks if it is Xin Database, find it and do not insert a duplicate.

So, of course, this needs to be synchronized, for example:

Let X = some value
Let Database = some external Database handle

LockMutex()
if !Database.contains(X):
   SomeCalculation()
   Database.insert(X)
UnlockMutex()

, , , . Mutex , . , - "" . (, Database .)

, ?

, , , , .

, SQL, - NoSQL, - , . , -, Atomic Stored Procedures? ? -, - "Distributed Mutex"? , , , , ?

, , , , .

+5
3

. . .

, . , , .

Concurrency. , , , . , -. concurrency .

, , , , , - . Via Optimistic concurrency ( SQL, SQL), , . , , .

. concurrency .

OCC ( - , ), .

+2

, "synch" DB, .

( , , - ), , .

0

, , . , : "" , . :

Let X = some value
Let Database = some external Database handle

xResposible = Definer.defineResponsibleFor(X);

if(xResposible == me)
    if !Database.contains(X):
       SomeCalculation()
       Database.insert(X)

, defineResponsibleFor , . , X Definer, . , . , ( , Definer - ). anyawy...:)

0

All Articles