Transaktioner och felhantering i 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 |
Ursprungligt saldo konto 5 = $35 000
Transaktion 1
BEGIN TRAN
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Transaktion 2
...
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
Måste vänta!
Ursprungligt saldo konto 5 = $35 000
Transaktion 1
BEGIN TRAN
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;

COMMIT TRAN;
Transaktion 2
...
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 30000,00 |
Transaktion 1
BEGIN TRAN
SELECT current_balance
FROM accounts WHERE account_id = 5;
Transaktion 2
...
...
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00 |
Fördelar:
Nackdelar:
När ska du använda det?:
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 |
Transaktion 1
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
BEGIN TRAN
SELECT current_balance FROM accounts
WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00 |
Transaktion 2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Måste vänta!
Transaktion 1
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
Transaktion 2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
Måste vänta!
Transaktion 1
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
Transaktion 2
...
...
UPDATE accounts
SET current_balance = 30000
WHERE account_id = 5;
(1 rows affected)
Fördelar:
Nackdelar:
REPEATABLE READ-transaktion.När ska du använda det?:
Transaktioner och felhantering i SQL Server