我有一个TRY CATCH语句,其中TRY中事务中有一些代码,而TRY中事务中有其他代码。如果转换之外的代码(仍在TRY中)抛出错误,则我的事务不会在CATCH中回滚。但是,如果我删除了事务外的代码,则如果出现错误,事务将被回滚。为什么这样表现呢? SQL Server 2014。
我的代码。如果Remote proc引发错误,则事务不会回滚:
BEGIN try
begin transaction
INSERT INTO bos_south_florida_job_map (job_id, original_job_id,
created_date, updated_date,completed_status_sent_ind,
assigned_status_sent_ind, status_prev)
VALUES (9999, '1234', getdate(), getdate(),0,0,'CREATED');
COMMIT TRANSACTION
declare @sql varchar(max)
set @sql = ''
select @sql = '
declare @error1 varchar(255),
@error2 varchar(255),
@error3 varchar(255)
Exec NEXTGEN.DBO.wbAf_ConfirmDispatchedReservation ''AFFWEB'', ''A10596'', ''Admin'', ''Admin'', '''+cast(preassignedsubconcode as varchar(100))+''', '''+cast('1234' as varchar(20))+''', '''+convert(char(23),ISNULL(ScheduledDispatchDateTime,''),121)+''', '+cast('1234' as varchar(12))+',null,@error1 output,@error2 output,@error3 output
if @error1 is not null or @error2 is not null or @error3 is not null
begin
set @error1 = @error2 + '' '' + @error3 + '' '' + @error1
RAISERROR (@error1, 16, 1)
end
'
from [BCCUATWSQL290].NEXTGEN.DBO.tbRideResCurDispatch
where resno = '35002616'
exec(@sql) at [BCCUATWSQL290]
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
INSERT INTO bos_south_florida_error_log (original_job_id, [action],
error_datetime, [error_message])
SELECT
'1234',
'CREATE_JOB',
GETDATE(),
'Msg: ' + ISNULL(CONVERT(VARCHAR, ERROR_NUMBER()), 'N/A') + ', Level: ' + ISNULL(CONVERT(VARCHAR, @ErrorSeverity), 'N/A') + ', Line: ' + ISNULL(CONVERT(VARCHAR, ERROR_LINE()), 'N/A') + ', Error: ' + ISNULL(@ErrorMessage, 'N/A')
END CATCH
这将回滚事务:
BEGIN try
begin transaction
INSERT INTO bos_south_florida_job_map (job_id, original_job_id,
created_date, updated_date,completed_status_sent_ind,
assigned_status_sent_ind, status_prev)
VALUES (9999, '1234', getdate(), getdate(),0,0,'CREATED');
RAISERROR ('BLAH', 16, 1)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE()
INSERT INTO bos_south_florida_error_log (original_job_id, [action],
error_datetime, [error_message])
SELECT
'1234',
'CREATE_JOB',
GETDATE(),
'Msg: ' + ISNULL(CONVERT(VARCHAR, ERROR_NUMBER()), 'N/A') + ', Level: ' + ISNULL(CONVERT(VARCHAR, @ErrorSeverity), 'N/A') + ', Line: ' + ISNULL(CONVERT(VARCHAR, ERROR_LINE()), 'N/A') + ', Error: ' + ISNULL(@ErrorMessage, 'N/A')
END CATCH
答案 0 :(得分:1)
回想一下,每个Begin Transaction
将@@Trancount
增加1,每个Commit
减少1。
在您的第一个示例中,事务已经提交,并且在引发错误之前@@TranCount
减小为零,因此在Catch
子句中,@@Trancount
将为零。很好提交的事务已经是历史记录,无法回滚。
在第二个示例中,当您在Begin
之后但在Commit
之前引发错误时,到达@@TranCount
子句并回滚时,Catch
为1。 / p>
大概您想要的行为是远程调用中的错误应导致回滚?您可以通过将Commit
移至远程调用之后,使其成为End Try
之前的最后一条语句来实现。
但是请注意,跨服务器事务比较昂贵,并且要求MS DTC在两个服务器上都运行。在负载很重的服务器上,DBA可能不会这样做。
顺便说一句,围绕单个插入的事务通常是没有意义的。正常行为是“自动提交”模式,这意味着插入/更新/删除语句为'bounded by an unseen BEGIN TRANSACTION and an unseen COMMIT TRANSACTION statement'