Transazioni e gestione degli errori in SQL Server
Miriam Antona
Software Engineer
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
| Dirty read | Non-repeatable read | Phantom read | |
|---|---|---|---|
| READ UNCOMMITTED | sì | sì | sì |
| READ COMMITTED | no | sì | sì |
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!
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 |
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 |
Pro:
Contro:
Quando usarlo?
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
| Dirty read | Non-repeatable read | Phantom read | |
|---|---|---|---|
| READ UNCOMMITTED | sì | sì | sì |
| READ COMMITTED | no | sì | sì |
| REPEATABLE READ | no | no | sì |
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!
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!
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)
Pro:
Contro:
REPEATABLE READ.Quando usarlo?
Transazioni e gestione degli errori in SQL Server