Given a list of database transactions, find all read-write conflicts among them. Assume that there is no strict two-phase locking (Strict 2PL) protocol to prevent read-write conflicts.

Each database transaction is given in the form of a tuple. The tuple ('T', 'A', 't', 'R') indicates that a transaction T accessed a database record A at a time t, and a read operation is performed on the record.

Assume that a data conflict happens when two transactions access the same record in the database within an interval of 5 units. At least one write operation is performed on the record.

 
For example,

Input:
 
(T1, A, 0, R)
(T2, A, 2, W)
(T3, B, 4, W)
(T4, C, 5, W)
(T5, B, 7, R)
(T6, C, 8, W)
(T7, A, 9, R)
 
Output:
 
Transaction T1 and T2 are involved in RW conflict
Transaction T3 and T5 are involved in WR conflict
Transaction T4 and T6 are involved in WW conflict

Practice this problem

Brief Overview on Read–Write conflicts

In databases, data conflict may arise during a read or write operation by the different transactions on the same data during the transaction’s life, leading to an inconsistent final database state. There are three types of data conflicts involved in the database transaction.

  • Write–Read (WR) Conflict (Dirty Read)
     
    This conflict occurs when a transaction reads the data which is written by the other transaction, but not yet committed.
     
  • Read–Write (RW) Conflict
     
    This conflict occurs when a transaction writes the data which is previously read by the other transaction.
     
  • Write–Write (WW) Conflict (Blind Write Operation)
     
    This conflict occurs when the data updated by a transaction is overwritten by another transaction which might lead to data update loss.

Note that there is no Read–Read (RR) conflict in the database since no information is updated in the database during the read operation.

Back to our Problem…

The idea is to sort all transactions in increasing order of database records and record access time. For each database record, consider all transaction pairs that have accessed the current record within an interval of 5 units. Finally, print all the conflicting transaction pairs for which at least one write operation is performed on the current record.

This can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output:
 
Transaction T1 & T2 are involved in RW conflict
Transaction T3 & T5 are involved in WR conflict
Transaction T4 & T6 are involved in WW conflict

The time complexity of the above solution is O(n.log(n)), where n is the total number of transactions. The time complexity appears to be O(n2) on the first look as a nested loop is involved. However, since the inner loop runs a maximum of 5 times (a constant value), the nested loop executes linearly.