[英]Multiple connections to the same DB in the same TransactionScope
How are the connections handled in a single transaction when we open close connections for each statement in C#?当我们为 C# 中的每个语句打开关闭连接时,如何在单个事务中处理连接?
The scenario is with the same connection string and the connection is opened and closed multiple types, once for every statement.该场景使用相同的连接字符串,并且连接打开和关闭多种类型,每个语句一次。
Consider the following example考虑以下示例
void updateSomething() {
using (SqlConnection connection = new SqlConnection(
"Integrated Security=SSPI;Initial Catalog=Northwind"))
{
connection.Open();
// Execute the statements
connection.Close();
}
}
When I'm executing the following code:当我执行以下代码时:
void SomeMethod()
{
using(TransactionScope scope = new TransactionScope())
{
for(int i=0; i < 10; i++)
{
this.updateSomething();
}
scope.Complete();
}
}
The recommendation is to use a connection Open/Close for each statement.建议对每个语句使用连接打开/关闭。 That is because we are not actually creating connections, we are just using one from the pool.
那是因为我们实际上并没有创建连接,我们只是使用池中的一个。
Why is this the case?为什么会这样? I get it that we hold the connection for as little time as we can, but the thing is that in most transactions, we are going to get it in the next moment during the next statement.
我知道我们保持连接的时间越短越好,但问题是在大多数交易中,我们将在下一个语句的下一刻得到它。
Is it only to avoid the demanding code computation time in between the statements if some such exists (which it shouldn't as it would lock the database in the transaction state for much longer that needed).如果存在一些这样的语句,是否只是为了避免语句之间要求的代码计算时间(它不应该因为它会将数据库锁定在事务 state 中所需的时间更长)。
Wouldn't it make sense to keep one connection open for the duration of the transaction?在事务期间保持一个连接打开是否有意义?
The recommendation is to use a connection Open/Close for each statement.
建议对每个语句使用连接打开/关闭。
Without seeing that comment in context, I'm guessing this recommendation is because of what you said: creating and destroying a SqlConnection
objects does not mean you are creating and destroying network connections.如果没有在上下文中看到该评论,我猜这个建议是因为您所说的:创建和销毁
SqlConnection
对象并不意味着您正在创建和销毁网络连接。
I think the motive behind "use one for each statement" is to just not worry about trying to be efficient about when you create SqlConnection
objects.我认为“为每个语句使用一个”背后的动机是不要担心在创建
SqlConnection
对象时尝试提高效率。 Don't go out of your way to keep one alive and pass it around all throughout your code thinking you are avoiding tearing down a network connection.不要让 go 让一个人活着,并在整个代码中传递它,以为您正在避免断开网络连接。 There's just no point.
没有意义。
In your example, it won't really make a difference.在您的示例中,它不会真正有所作为。 You can use the same
SqlConnection
object for each query if you'd like, as long as you are making them sequentially and not in parallel.如果您愿意,可以对每个查询使用相同的
SqlConnection
object,只要您按顺序而不是并行进行。 It's probably even slightly more efficient since you save the computational time of creating an SqlConnection
object.由于您节省了创建
SqlConnection
object 的计算时间,因此效率可能会更高一些。 But that saved time will likely not even be noticeable.但是,节省的时间可能甚至不会被注意到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.