作为 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.
基于以上,我有两个问题:
- 是否可以安全地假设已登记的连接永远不会在另一个事务中使用,只要该事务存在,即使它已关闭?是否有任何文件可以解释我们的假设是否如此?
- 如果连接仅用于登记的事务中,它怎么可能不可用?这可能是什么情况?
最佳答案
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/