关闭连接后,未提交的事务会如何处理? [英] What happens to an uncommitted transaction when the connection is closed?

查看:669
本文介绍了关闭连接后,未提交的事务会如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们会立即回滚吗?
一段时间后它们会回滚吗?
它们是否处于未提交状态?

Are they rolled back immediately? Are they rolled back after some period of time? Are they left in an uncommitted state?

如果使用连接池并且仅重置连接,行为是否相同?

Is the behavior the same if connection pooling is used and the connections are simply reset?

推荐答案

在应用连接池时,它可以保持打开状态。例如:命令超时可能会留下锁和TXN,因为客户端发送的是中止消息。

It can stay open while connection pooling applies. Example: command timeout can leave locks and TXN because the client sends as "abort".

2个解决方案:


  • 在客户端中进行字面测试:

  • Test in the client, literally:

如果@@ TRANCOUNT<> 0 ROLLBACK TRAN

使用 SET XACT_ABORT ON 来确保TXN为清理:问题1 问题2

Use SET XACT_ABORT ON to ensured a TXN is cleaned up: Question 1 and Question 2

我总是使用 SET XACT_ABORT ON

来自此SQL团队博客


请注意,在使用连接池的情况下,
仅在没有
a回滚的情况下关闭连接只会返回
与池的连接以及
事务将保持打开状态,直到
随后被重新使用或从池中删除为止。
这可能导致锁开始不必要地持有
并导致其他超时
和滚动块

Note that with connection pooling, simply closing the connection without a rollback will only return the connection to the pool and the transaction will remain open until later reused or removed from the pool. This can result in locks begin held unnecessary and cause other timeouts and rolling block

MSDN ,交易支持部分(我粗体)

From MSDN, section "Transaction Support" (my bold)


关闭连接后,会将
释放回池中,并将其细分为
基于
的交易环境。因此,即使
分布式事务仍在
待处理中,
仍可以关闭连接,而
不会产生错误,这样,您便可以在以后
的时间提交或
中止分布式事务。

这篇关于关闭连接后,未提交的事务会如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆