c# - C# 中的连接登记到底是什么

标签 c# transactions

作为 Multiple connections to the same DB in the same TransactionScope 的跟进, 我试图找出以下 in this guide here 的确切含义:

Connections are drawn from the pool and assigned based on transaction context. Unless Enlist=false is specified in the connection string, the connection pool makes sure that the connection is enlisted in the Current context. When a connection is closed and returned to the pool with an enlisted System.Transactions transaction, it is set aside in such a way that the next request for that connection pool with the same System.Transactions transaction will return the same connection if it is available.

在另一节中提到

Connections are drawn from the pool and assigned based on transaction context.

When a connection is closed, it is released back into the pool and into the appropriate subdivision based on its transaction context.

基于以上,我有两个问题:

  1. 是否可以安全地假设已登记的连接永远不会在另一个事务中使用,只要该事务存在,即使它已关闭?是否有任何文件可以解释我们的假设是否如此?
  2. 如果连接仅用于登记的事务中,它怎么可能不可用?这可能是什么情况?

最佳答案

Is it safe to assume that an enlisted connection, will NEVER be used in another transaction as long as the transaction exists, even though it's closed?

是的。

Is there any documentation explaining if our assumptions that this is so?

您引用了它:“连接是从池中提取的,并根据事务上下文进行分配。”

How can a connection NOT be available if it is only used in the enlisted transaction? What can be the case for this?

它可以在使用中。例如在 TransactionScope 中:

using (var con = new SqlConnection(...))
{
   con.Open();
   using (var con2 = new SqlConnection(...))  
   {
     con2.Open(); //con is not available, as it's open and in-use so a new connection will be opened and enlisted
   }
}

关于c# - C# 中的连接登记到底是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58958624/

相关文章:

php - 将数据从一个 MySQL 表传输到另一个

java - 用于 HA 的 Neo4j-ogm X-Write header 管理

sql - TRANSACTION ISOLATION LEVEL SERIALIZABLE 是否会创建 READ 锁

java - 在露天。如何创建临时文件进行测试?

javascript - 在文本区域中的文本中显示用户选择的文本

c# - 如何让异常在 C# 中定义自己的消息?

java - readOnly=true 和 TransactionType Never 有什么区别?

c# - 递归是确定目录中最大文件大小的最佳选择吗

c# - 如何动态更改 wpf MVVM light 中用户控件中存在的按钮(单击)上的用户控件

c# - C#反射(reflection):如何使用“对象”作为“类型参数”