Now, we shall look at an example of concurrent execution of two transactions controlled by two-phase locking protocol. To simplify the protocol we consider a single "lock" operations instead of "shared lock" and "exclusive lock".
A transaction T1 starts processing and attempts to access a data item u with an operation a=read(u). A scheduler locks a data item u and grants lock to a transaction T1. Then a transaction T1 process an operation a=read(u).
Next, a transaction t2 attempts to access a data item v and it obtains a lock on v from the scheduler. With lock granted a transaction T2 processes and operation write(v, 10).
Next, a transaction t2 attempts to access a data item v and it obtains a lock on v from the scheduler. With lock granted a transaction T2 processes and operation write(v, 10).
Next a transaction T1 attempts to lock a data item v. A data item v is already locked by a transaction T2 and a transaction T1 must wait for a lock.
Next, a transaction T2 gets a lock on a data item x and it reads a value of data item x with b=read(x).
A transaction T1 still waits for a lock on a data item v.
Next a transaction T2 releases a lock on a data item v. Due to "two-phase" protocol a transaction T2 ends its "locking phase" and it is not allowed to lock any more data items. A data item is unlocked.
Next, a transaction T2 write a new value into a data item x.
Next, a transaction T1 locks a data item v earlier unlocked by a transaction T2.
Next, a transaction T2 unlocks a data item x.
Next, a transaction T1 writes a new value into a data item v.
Finally a transaction T1 unlocks the data items v and u. The first unlock of data item v ends a locking phase of transactions T1 and it starts unlocking phase of a transaction T1.
Please note the locking phases and unlocking phases of both transactions.