XACT_ABORT e XACT_STATE

Transazioni e gestione degli errori in SQL Server

Miriam Antona

Software Engineer

XACT_ABORT

Indica se la transazione corrente verrà annullata automaticamente quando si verifica un errore.

SET XACT_ABORT { ON | OFF }
SET XACT_ABORT OFF
  • Impostazione predefinita
  • In caso di errore: possono restare transazioni aperte
SET XACT_ABORT ON
  • In caso di errore: esegue il rollback della transazione e interrompe l’esecuzione
Transazioni e gestione degli errori in SQL Server

XACT_ABORT - esempi

SET XACT_ABORT OFF; --Default setting

BEGIN TRAN;
    INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
    INSERT INTO customers VALUES ('Dylan', 'Smith', '[email protected]', '555888999'); -- ERROR!
COMMIT TRAN;
(1 row affected)
Msg. 2627, Level 14, State 1, Line 5
Violation of UNIQUE KEY 'unique_email'...
| customer_id | first_name | last_name | email                | phone     |
|-------------|------------|-----------|----------------------|-----------|
| 14          | Mark       | Davis     | [email protected]   | 555909090 |
Transazioni e gestione degli errori in SQL Server

XACT_ABORT - esempi

SET XACT_ABORT ON;

BEGIN TRAN;
    INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
    INSERT INTO customers VALUES ('Dylan', 'Smith', '[email protected]', '555888999'); -- ERROR!
COMMIT TRAN;
Msg. 2627, Level 14, State 1, Line 4
Violation of UNIQUE KEY 'unique_email'...
SELECT * FROM customers WHERE first_name = 'Mark';
| customer_id | first_name | last_name | email                | phone     |
|-------------|------------|-----------|----------------------|-----------|
Transazioni e gestione degli errori in SQL Server

XACT_ABORT CON RAISERROR

SET XACT_ABORT ON;
BEGIN TRAN;
    INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
    RAISERROR('Raising an error!', 16, 1);
    INSERT INTO customers VALUES ('Zack', 'Roberts', '[email protected]', '555919191');
COMMIT TRAN;
Msg. 50000, Level 16, State 1, Line 5
Raising an error!
SELECT * FROM customers WHERE first_name IN ('Mark', 'Zack');
| customer_id | first_name | last_name | email                | phone     |
|-------------|------------|-----------|----------------------|-----------|
| 14          | Mark       | Davis     | [email protected]   | 555909090 |
| 15          | Zack       | Roberts   | [email protected] | 555919191 |
Transazioni e gestione degli errori in SQL Server

XACT_ABORT con THROW

SET XACT_ABORT ON;
BEGIN TRAN;
    INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
    THROW 55000, 'Raising an error!', 1;
    INSERT INTO customers VALUES ('Zack', 'Roberts', '[email protected]', '555919191');
COMMIT TRAN;
(1 rows affected)
Msg. 50000, Level 16, State 1, Line 5
Raising an error!
SELECT * FROM customers WHERE first_name IN ('Mark', 'Zack');
| customer_id | first_name | last_name | email | phone |
|-------------|------------|-----------|-------|-------|
Transazioni e gestione degli errori in SQL Server

XACT_STATE

XACT_STATE()
  • 0 -> nessuna transazione aperta
  • 1 -> transazione aperta e confermabile
  • -1 -> transazione aperta e non confermabile (doomed)
    • non può fare commit
    • non può fare rollback a un savepoint
    • può fare rollback dell’intera transazione
    • non può modificare/può solo leggere
Transazioni e gestione degli errori in SQL Server

XACT_STATE - aperta e confermabile

SET XACT_ABORT OFF;
BEGIN TRY
    BEGIN TRAN;
        INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
        INSERT INTO customers VALUES ('Dylan', 'Smith', '[email protected]', '555888999'); -- ERROR!
    COMMIT TRAN;
END TRY
BEGIN CATCH
    IF XACT_STATE() = -1
        ROLLBACK TRAN;
    IF XACT_STATE() = 1
        COMMIT TRAN;
    SELECT ERROR_MESSAGE() AS error_message;
END CATCH
| error_message                               |
|---------------------------------------------|
| Violation of UNIQUE KEY 'unique_email'...   |
Transazioni e gestione degli errori in SQL Server

XACT_STATE - aperta e confermabile

| customer_id | first_name | last_name | email                | phone     |
|-------------|------------|-----------|----------------------|-----------|
| 14          | Mark       | Davis     | [email protected]   | 555909090 |
Transazioni e gestione degli errori in SQL Server

XACT_STATE - aperta e non confermabile (doomed)

SET XACT_ABORT ON;
BEGIN TRY
    BEGIN TRAN;
        INSERT INTO customers VALUES ('Mark', 'Davis', '[email protected]', '555909090');
        INSERT INTO customers VALUES ('Dylan', 'Smith', '[email protected]', '555888999'); -- ERROR!
    COMMIT TRAN;
END TRY
BEGIN CATCH
    IF XACT_STATE() = -1
        ROLLBACK TRAN;
    IF XACT_STATE() = 1
        COMMIT TRAN;
    SELECT ERROR_MESSAGE() AS Error_message;
END CATCH
Transazioni e gestione degli errori in SQL Server

XACT_STATE - aperta e non confermabile (doomed)

SELECT * FROM customers WHERE first_name = 'Mark';
| customer_id | first_name | last_name | email                | phone     |
|-------------|------------|-----------|----------------------|-----------|
Transazioni e gestione degli errori in SQL Server

Passons à la pratique !

Transazioni e gestione degli errori in SQL Server

Preparing Video For Download...