Lições do módulo (4/4)
Níveis de isolamento
Beyond explicit FOR UPDATE, transactions also have innate mathematical rules baked deep into the engine that govern how much energy to spend isolating each individual interaction by default. This is called the Isolation Level. In the SQL world we know 4 standard ones, in order of power and safety but obviously the inverse in terms of performance.
QUERY LENGTH LIMIT EXCEEDED. MAX ALLOWED QUERY : 500 CHARS
How do you set it?
Instead of the normal BEGIN; you type:
BEGIN ISOLATION LEVEL SERIALIZABLE;
-- and you proceed normally with your hermetically sealed isolation
UPDATE products SET stock = 10;
COMMIT;Or in some variants and older engines you assign it after starting:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;Try it yourself
Start a transaction formally setting the global level for the lifetime of the query to REPEATABLE READ to create a frozen, protected exchange.\nRemember that at the query level it serves no purpose unless followed up by something else, so close it empty with 'COMMIT'.
Mostrar dica
Open with BEGIN ISOLATION LEVEL REPEATABLE READ; and close with COMMIT;
Solução disponível após 3 tentativas
Let's try the crazy serializable level and insert some fictitious data.\nOpen a 'SERIALIZABLE' block.\nUpdate the name of category Id=2 to 'Nuovo Nome Segreto'.\nRun the final atomic, settling commit.
Mostrar dica
BEGIN ISOLATION LEVEL SERIALIZABLE;\nUPDATE categories SET name = 'Nuovo Nome Segreto' WHERE id = 2;\nCOMMIT;
Solução disponível após 3 tentativas