Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions install/05_delta_framework.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1202,38 +1202,43 @@ BEGIN

END TRY
BEGIN CATCH
DECLARE
@error_message nvarchar(4000) = ERROR_MESSAGE();

/*
Only rollback if we started the transaction
Otherwise let the caller handle it
*/
IF @trancount_at_entry = 0
IF @trancount_at_entry = 0
AND @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;

DECLARE
@error_message nvarchar(4000) = ERROR_MESSAGE();

/*
Log the error
Log the error only if the transaction is not doomed
When called inside a caller's transaction that is doomed (XACT_STATE = -1),
we cannot write to the log — the caller must rollback first
*/
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
duration_ms,
error_message
)
VALUES
(
N'calculate_deltas_' + @table_name,
N'ERROR',
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
@error_message
);

IF XACT_STATE() <> -1
BEGIN
INSERT INTO
config.collection_log
(
collector_name,
collection_status,
duration_ms,
error_message
)
VALUES
(
N'calculate_deltas_' + @table_name,
N'ERROR',
DATEDIFF(MILLISECOND, @start_time, SYSDATETIME()),
@error_message
);
END;

RAISERROR(N'Error calculating deltas for %s: %s', 16, 1, @table_name, @error_message);
END CATCH;
END;
Expand Down
11 changes: 6 additions & 5 deletions install/06_ensure_collection_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,14 @@ BEGIN
BEGIN CATCH
SET @error_message = ERROR_MESSAGE();

IF @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END;

/*
Log errors to collection log
Must happen after rollback to avoid doomed transaction writes
*/
INSERT INTO
config.collection_log
Expand All @@ -1229,11 +1235,6 @@ BEGIN
@error_message
);

IF @@TRANCOUNT > 0
BEGIN
ROLLBACK;
END;

THROW;
END CATCH;
END;
Expand Down
Loading