Sobes.tech
Senior

Какой побочный эффект возникает при выполнении кода BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED; START TRANSACTION; SELECT progress INTO @current_progress FROM courses WHERE course_id = 101; SET @current_progress = @current_progress + 60; UPDATE courses SET progress = @current_progress WHERE course_id = 101; COMMIT; END; BEGIN; SET TRANSACTION ISOLATION LEVEL READ COMMITTED; START TRANSACTION; SELECT progress INTO @current_progress FROM courses WHERE course_id = 101; SET @current_progress = @current_progress + 40; UPDATE courses SET progress = @current_progress WHERE course_id = 101; COMMIT; END;

sobes.tech AI

Answer from AI

The provided code contains two parallel transactional blocks that read and update the progress field in the courses table for the same course_id = 101.

Since the isolation level is set to READ COMMITTED, a race condition can occur: both transactions read the same progress value, then increase it by 60 and 40 respectively, and write it back.

The side effect is a lost update. For example, if initially progress = 0, both transactions will read 0, the first will add 60 and write 60, the second will add 40 and write 40, overwriting the first's result. As a result, the final value will be 40 instead of the expected 100.

To prevent this, a stricter isolation level (such as SERIALIZABLE) should be used, or locking mechanisms/optimistic locking should be applied.