Pular para o conteúdo principal
Módulo 11 · Lição 4 de 444/57 no curso~12 min
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:

SQL
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:

SQL
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;

Try it yourself

Exercício#sql.m11.l4.e1
Tentativas: 0Carregando…

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'.

Carregando editor…
Mostrar dica

Open with BEGIN ISOLATION LEVEL REPEATABLE READ; and close with COMMIT;

Solução disponível após 3 tentativas

Exercício#sql.m11.l4.e2
Tentativas: 0Carregando…

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.

Carregando editor…
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