Search code examples
ordbms

How is concurrency control implemented in any ORDBMS


I have a weird question about concurrency control in ORDBMS. This is completely theoretical.

I have two transactions T1 and T2 trying to update a particular row on a table. Now both the transactions T1 and T2 hits the database simultaneously. By simultaneously, I mean both hits at the same time calculated till nanoseconds.

So if both the transactions have a timestamp that is exactly same, then how does a DBMS (be it Oracle, DB2, SQL Server) identifies which transaction to process first and which transaction to process later.

I understand that a row level lock will be achieved by one transaction and the other will wait till the lock is released. But how will it identify whether T1 or T2 will acquire the lock. Is there some other parameter that is taken into account other than timestamp.

Thanks Nirmalya


Solution

  • This question seems to be related more to concurrency control of DBMS in general, rather then of ORDBMS.

    Anyway, as far as I know, even if two requests are issued exactly at the same time, they will be processed sequentially by the scheduler, which is responsible for acquiring locks and assigning timestamps. Obviously, only the scheduler is sequential: after scheduling, the queries can be processed parallely, if this is allowed by the locks and by timestamps ordering.

    Wikipedia has an accurate explanation of timestamp-based concurrency control: https://en.wikipedia.org/wiki/Timestamp-based_concurrency_control. There you can see that there are few assumptions to be made. Look at the first two:

    • Every timestamp value is unique and accurately represents an instant in time.
    • No two timestamps can be the same.

    These assumption can be guaranteed only using a thread-safe scheduler, which assings timestamps to transactions sequentially. Also in lock-based concurrency control the scheduler must be thread-safe. Otherwise when it locks a record it cannot be sure that no another transactions acquired a lock on the same record.