您发现该存储过程模板有什么问题吗?
所以我创建了(我使用了在其他网站上找到的一些东西)来处理事务并在执行存储过程时进行某种堆栈跟踪,该存储过程可以调用需要事务等的其他存储过程,
所以如果我有 A 调用 B 且 B 正在调用 C 且 C 出现错误,我可以正确 回滚我的东西并返回一个堆栈跟踪说:C 中的错误按照跟踪找出在哪里/如何/等等...
你们中有人发现这个逻辑有问题吗?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[NAME]
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
declare @trancount int
set @trancount = @@trancount
declare @savePointName varchar(40)
set @savePointName = newid()
BEGIN TRY
if @trancount = 0
begin transaction
else
save transaction @savePointName
/*
// STUFF HERE
*/
if @trancount = 0
commit transaction
END TRY
BEGIN CATCH
declare @xstate int
set @xstate = XACT_STATE()
if @xstate = -1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount > 0
rollback transaction @savePointName
declare @message varchar(max)
set @message = ERROR_MESSAGE() +
' (' + ERROR_PROCEDURE() +
':' + ltrim(str(ERROR_LINE())) +
', Raised ' + ltrim(str(ERROR_NUMBER())) +
', Severity ' + ltrim(str(ERROR_SEVERITY())) +
', State ' + ltrim(str(ERROR_STATE())) + ')'
RAISERROR(@message,16,1)
END CATCH
END
so I created that(I used some stuff found on other website) to handle transactions and having a sort of stacktrace while executing stored procedure that could call other stored procedure that need transaction and etc.
so if I have A calling B and B is calling C and C got an error, I can correctly rollback my stuff and returning a stacktrace saying: error in C follow the trace to find out where/how/etc...
do any of you find a problem with this logic?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[NAME]
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
declare @trancount int
set @trancount = @@trancount
declare @savePointName varchar(40)
set @savePointName = newid()
BEGIN TRY
if @trancount = 0
begin transaction
else
save transaction @savePointName
/*
// STUFF HERE
*/
if @trancount = 0
commit transaction
END TRY
BEGIN CATCH
declare @xstate int
set @xstate = XACT_STATE()
if @xstate = -1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount > 0
rollback transaction @savePointName
declare @message varchar(max)
set @message = ERROR_MESSAGE() +
' (' + ERROR_PROCEDURE() +
':' + ltrim(str(ERROR_LINE())) +
', Raised ' + ltrim(str(ERROR_NUMBER())) +
', Severity ' + ltrim(str(ERROR_SEVERITY())) +
', State ' + ltrim(str(ERROR_STATE())) + ')'
RAISERROR(@message,16,1)
END CATCH
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,我看不出这段代码有什么问题。
No, I can't spot anything wrong with this code.