How to rollback a transaction in a stored procedure?

BEGIN TRANSACTION;

BEGIN TRY
-- Some code
COMMIT TRANSACTION;
END TRY
BEGIN CATCH ROLLBACK TRANSACTION;
END CATCH;

执行的存储过程需要transaction的话,在调用的时候传入

BEGIN TRANSACTION
DECLARE @usedrecords XML;
SET @usedrecords = N'<Record ID="388" />';
EXEC dbo.pd_trn_Transaction @UsedRecords

如果有事务锁定了数据库,那么可以直接rollback

ROLLBACK TRAN

在执行存储过程的的时候,错误提示

You attempted to acquire a transactional application lock without an active transaction.

 DECLARE @res INT;
EXEC @res = sp_getapplock @Resource = 'TransactionCheckLock' ,
@LockMode = 'Exclusive' ,
@LockOwner = 'Transaction' ,
@LockTimeout = 10000 ,
@DbPrincipal = 'public';
IF @res NOT IN ( 0, 1 )
BEGIN
RETURN -2;
END;

存储过程内部,要求TransactionCheckLock,所以在外部调用的时候,必须加上begin transaction

sql try/catch rollback/commit - preventing erroneous commit after rollback

I always thought this was one of the better articles on the subject.

It includes the following example that I think makes it clear and includes the frequently overlooked @@trancount which is needed for reliable nested transactions

PRINT 'BEFORE TRY'
BEGIN TRY
BEGIN TRAN
PRINT 'First Statement in the TRY block'
INSERT INTO dbo.Account(AccountId, Name , Balance) VALUES(1, 'Account1', 10000)
UPDATE dbo.Account SET Balance = Balance + CAST('TEN THOUSAND' AS MONEY) WHERE AccountId = 1
INSERT INTO dbo.Account(AccountId, Name , Balance) VALUES(2, 'Account2', 20000)
PRINT 'Last Statement in the TRY block'
COMMIT TRAN
END TRY
BEGIN CATCH
PRINT 'In CATCH Block'
IF(@@TRANCOUNT > 0)
ROLLBACK TRAN; THROW; -- raise error to the client
END CATCH
PRINT 'After END CATCH'
SELECT * FROM dbo.Account WITH(NOLOCK)
GO

查询没有处理的Transaction

https://stackoverflow.com/questions/3978227/how-to-kill-or-rollback-active-transaction

SELECT * FROM sys.dm_tran_session_transactions 

You can't kill/rollback a transaction from another session without killing the owner session.

I think, allowing to kill/rollback a transaction from another user's session means many design and security rule violations because it requires entering another user session (in the context of the current sql server engine design). That's probably why it is not implemented.

关于如何kill session

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/kill-transact-sql?view=sql-server-2017

最新文章

  1. RequireJS实例分析【转】
  2. jQuery的jsonp跨域是这么回事.
  3. Linux系统文件权限&amp;目录权限
  4. OpenGL 顶点缓存对象
  5. &quot;_ITERATOR_DEBUG_LEVEL&quot;的不匹配项: 值&quot;0&quot;不匹配值&quot;2&quot;
  6. Source Insight的应用技巧、宏功能
  7. DB面试题
  8. php排序之快速排序
  9. Eclipse默认配色的恢复方法
  10. css-下拉菜单案例
  11. cloud computing platform,virtual authentication encryption
  12. poj3352添加多少条边可成为双向连通图
  13. Ajax状态值及状态码整理
  14. 【python游戏编程04--加载位图与常用的数学函数】
  15. .12-浅析webpack源码之NodeWatchFileSystem模块总览
  16. Spark进阶之路-Spark HA配置
  17. 2017-2018-2 20165303 实验三《Java面向对象程序设计》实验报告
  18. Mysql的select in会自动过滤重复的数据
  19. Android Studio preview 不显示,程序运行正常
  20. linux System V IPC Mechanisms

热门文章

  1. IE模式下EasyUI Combobox无效问题
  2. vue之computed和watch
  3. OpenJDK源码研究笔记(二)-Comparable和Comparator2个接口的作用和区别(一道经典的Java笔试面试题)
  4. Qt之QPushButton
  5. 如何将visual studio 2010编辑模式改为插入???
  6. 本地搭建MongoDB Server
  7. Pascal Script
  8. 智课雅思短语---四、Exploit to the full one’s favorableconditions and avoid unfavorable ones
  9. JS实现文字图片无缝滚动
  10. MySQL优化-存储引擎