我真的需要使用“SET XACT_ABORT ON"吗?

如果你很小心并且在所有事情上都使用 TRY-CATCH 并且回滚错误,你真的需要使用:

if you are careful and use TRY-CATCH around everything, and rollback on errors do you really need to use:

SET XACT_ABORT ON

换句话说,是否有任何 TRY-CATCH 会错过 SET XACT_ABORT ON 将处理的错误?

In other words, is there any error that TRY-CATCH will miss that SET XACT_ABORT ON will handle?

推荐答案

请记住,无论是否使用 XACT_ABORT,TRY-CATCH 都不会捕获某些错误.

Remember that there are errors that TRY-CATCH will not capture with or without XACT_ABORT.

但是,SET XACT_ABORT ON不影响捕获错误.它确实保证任何事务都会回滚/注定失败.当OFF"时,您仍然可以选择提交或回滚(取决于 xact_state).这是 SQL 2005 XACT_ABORT

如果客户端命令超时并且客户端发送中止"指令,它还会移除锁等.如果没有 SET XACT_ABORT,如果连接保持打开状态,锁可以保留.我的同事(MVP)和我在年初彻底测试了这个.

What it also does is remove locks etc if the client command timeout kicks in and the client sends the "abort" directive. Without SET XACT_ABORT, locks can remain if the connection remains open. My colleague (an MVP) and I tested this thoroughly at the start of the year.

相关文章