READ COMMITTED e REPEATABLE READ

Transazioni e gestione degli errori in SQL Server

Miriam Antona

Software Engineer

READ COMMITTED

  • Livello di isolamento predefinito
  • Non legge dati modificati da un'altra transazione non ancora committed o rollbaccata
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
Transazioni e gestione degli errori in SQL Server

READ COMMITTED - confronto livelli di isolamento

Dirty read Non-repeatable read Phantom read
READ UNCOMMITTED
READ COMMITTED no
Transazioni e gestione degli errori in SQL Server

READ COMMITTED - prevenire dirty read

Saldo iniziale conto 5 = $35.000

Transazione1

BEGIN TRAN
  UPDATE accounts
  SET current_balance = 30000
  WHERE account_id = 5;

Transazione2

...

...

...

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;

Deve aspettare!

Transazioni e gestione degli errori in SQL Server

READ COMMITTED - prevenire dirty read

Saldo iniziale conto 5 = $35.000

Transazione1

BEGIN TRAN
  UPDATE accounts
  SET current_balance = 30000
  WHERE account_id = 5;

COMMIT TRAN;

Transazione2

...

...

...

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 30000,00        |
Transazioni e gestione degli errori in SQL Server

READ COMMITTED - selezione senza attesa

Transazione1

BEGIN TRAN
  SELECT current_balance
  FROM accounts WHERE account_id = 5;

Transazione2

...

...

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00        |
Transazioni e gestione degli errori in SQL Server

READ COMMITTED - riepilogo

Pro:

  • Impedisce dirty read

Contro:

  • Consente non-repeatable read e phantom read
  • Puoi essere bloccato da un'altra transazione

Quando usarlo?

  • Vuoi leggere solo dati committed; accetti non-repeatable e phantom read
Transazioni e gestione degli errori in SQL Server

REPEATABLE READ

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
  • Non legge dati non committed di altre transazioni
  • Se leggi dei dati, altre transazioni non possono modificarli finché la REPEATABLE READ non termina
Transazioni e gestione degli errori in SQL Server

REPEATABLE READ - confronto livelli di isolamento

Dirty read Non-repeatable read Phantom read
READ UNCOMMITTED
READ COMMITTED no
REPEATABLE READ no no
Transazioni e gestione degli errori in SQL Server

REPEATABLE READ - prevenire non-repeatable read

Transazione1

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
    SELECT current_balance FROM accounts
    WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00        |

Transazione2

...

...

UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;

Deve aspettare!

Transazioni e gestione degli errori in SQL Server

REPEATABLE READ - prevenire non-repeatable read

Transazione1

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
    SELECT current_balance FROM accounts
    WHERE account_id = 5;
    SELECT current_balance FROM accounts
    WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00        |
COMMIT TRAN

Transazione2

...

...

UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;

Deve aspettare!

Transazioni e gestione degli errori in SQL Server

REPEATABLE READ - prevenire non-repeatable read

Transazione1

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
    SELECT current_balance FROM accounts
    WHERE account_id = 5;
    SELECT current_balance FROM accounts
    WHERE account_id = 5;
COMMIT TRAN

Transazione2

...

...

UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
(1 riga interessata)
Transazioni e gestione degli errori in SQL Server

REPEATABLE READ - riepilogo

Pro:

  • Impedisce ad altre transazioni di modificare i dati che stai leggendo; evita non-repeatable read
  • Evita dirty read

Contro:

  • Consente phantom read
  • Puoi essere bloccato da una transazione REPEATABLE READ.

Quando usarlo?

  • Vuoi leggere solo dati committed e impedire modifiche ai dati letti. Non ti preoccupano i phantom read
Transazioni e gestione degli errori in SQL Server

Ayo berlatih!

Transazioni e gestione degli errori in SQL Server

Preparing Video For Download...