Assuming the following code:
/*
DROP TABLE #Teste;
CREATE TABLE #Teste(id INT);
INSERT INTO #Teste VALUES(1);
CREATE TABLE LogErros(id INT IDENTITY(1,1) PRIMARY KEY, nomeTransaction VARCHAR(100), errorMessage VARCHAR(500), horaErro DATETIME)
DROP TABLE LogErros
SELECT * FROM LogErros
*/
SET XACT_ABORT ON
DECLARE @TranName VARCHAR(20) = 'TR_Teste';
BEGIN TRANSACTION @TranName
BEGIN TRY
INSERT INTO #Teste VALUES(2);
INSERT INTO #Teste VALUES(3);
--INSERT INTO #Teste VALUES('Oi');
COMMIT TRANSACTION @TranName
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION @TranName
INSERT INTO LogErros VALUES(@TranName, CAST(ERROR_LINE() as VARCHAR) + ' - ' + ERROR_MESSAGE(), GETDATE());
END CATCH
Is it worth using XACT_ABORT()
in such cases?
I saw in the international stackoverflow that the previous function can handle cases where the procedure suffers timeout
eg problem that TRY/CATCH
can not handle. Thinking about that case, then the use is valid, correct?
But I also saw people saying that XACT_ABORT()
was obsolete after TRY/CATCH
, is this true? So can TRY/CATCH
handle all the errors alone?