Middle+
How does Read Committed work at the level of two transactions? Provide an example.
sobes.tech AI
Answer from AI
The Read Committed isolation level guarantees that a transaction only sees data that has been committed by other transactions. This prevents dirty reads but does not protect against non-repeatable reads or phantom reads.
Example:
- Transaction T1 begins reading data.
- Transaction T2 modifies data and commits the changes.
- If T1 reads the same data again, it will see T2's changes.
Thus, with Read Committed, a transaction will not see uncommitted changes but may see changes made and committed by other transactions during its operation.
In Python, this behavior can be observed when working with databases through drivers like psycopg2 for PostgreSQL, by setting the isolation level:
import psycopg2
conn = psycopg2.connect(...)
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_READ_COMMITTED)