Transações e tratamento de erros no SQL Server
Miriam Antona
Software Engineer
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
| Dirty reads | Non-repeatable reads | Phantom reads | |
|---|---|---|---|
| READ UNCOMMITTED | yes | yes | yes |
| READ COMMITTED | no | yes | yes |
Saldo original da conta 5 = $35.000
Transação1
BEGIN TRAN
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Transação2
...
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
Tem que esperar!
Saldo original da conta 5 = $35.000
Transação1
BEGIN TRAN
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;

COMMIT TRAN;
Transação2
...
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 30000,00 |
Transação1
BEGIN TRAN
SELECT current_balance
FROM accounts WHERE account_id = 5;
Transação2
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00 |
Prós:
Contras:
Quando usar?:
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
| Dirty reads | Non-repeatable reads | Phantom reads | |
|---|---|---|---|
| READ UNCOMMITTED | yes | yes | yes |
| READ COMMITTED | no | yes | yes |
| REPEATABLE READ | no | no | yes |
Transação1
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
SELECT current_balance FROM accounts
WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00 |
Transação2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Tem que esperar!
Transação1
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
Transação2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Tem que esperar!
Transação1
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
Transação2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
(1 rows affected)
Prós:
Contras:
REPEATABLE READ.Quando usar?:
Transações e tratamento de erros no SQL Server