I'm studying Database Management System and I have a project that I need to demo Lost update, Uncommitted data, Unrepeatable data, Phantom in a DMS I selected. I selected MySQL for my project. But after a couple of times trying to demonstrate I failed. This is a case I tried.
This is transaction T1
START TRANSACTION;
UPDATE roles SET roles.isAdmin = '0';
DO sleep(5);
ROLLBACK;
This is transaction T2
START TRANSACTION;
SELECT * FROM roles WHERE 1;
COMMIT;
Can you help me to figure out how can I demonstrate Lost update, Uncommitted data, Unrepeatable data, Phantom situation?
Best Answer
The different problems are prevented at different isolation levels. So you need to first set an isolation level that allows a problem to happen before you can demonstrate it.
Here's a table that explains which isolation level allows which problem to occur (taken from my post over at stackoverflow which also briefly explains the problems the isolation levels are solving).
So, for the first example, the lost update problem, it would work like this:
The session is not yet committed, but in session 2 you can already see the value, when you set the isolation accordingly.
As you can see the
Changed
value for session 2 is0
, because it read uncommitted values from session 1. When you commit both sessions now, no matter which one first, the values in column a will be2
. The update statement of session 2 got lost.I think you get the idea, of how to demonstrate the problems. I got some work to do now. When you still have problems, I will update this post when I find the time.