简体   繁体   English

用ADO.NET编写的数据库事务实际上何时开始?

[英]When does a db transaction written in ADO.NET actually begin?

One of the key things in database-intensive applications is to keep the transactions as short as possible. 数据库密集型应用程序的关键之一是保持事务尽可能短。

Today I was wondering when this transaction would actually begin: 今天,我想知道这项交易何时真正开始:

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
    {
        sqlConnection.Open();
/*(1)*/ SqlTransaction sqlTransaction = sqlConnection.BeginTransaction(IsolationLevel.ReadUncommitted); 

        //Perform some stuff
        //...

/*(2)*/ using (SqlCommand command = new SqlCommand(sqlQuery, sqlConnection, sqlTransaction))  
        {
             //Some other stuff
             //...
             try
             {
                 /*(3)*/sqlCommand.ExecuteNonQuery();
                 //More irrelevant code
                 //...
                 sqlCommand.CommandText = otherQuery;
                 sqlCommand.ExecuteNonQuery();
                 sqlTransaction.Commit();
             }
             catch(Exception)
             {
                 sqlTransaction.Rollback();
                 throw;
             }
        }
  }

In step (1), (2) or (3)? 在步骤(1),(2)或(3)中? Ideally it should be in step 3. 理想情况下,它应该在步骤3中。

The transaction starts at point 3, the first time you exectue a command on the connection. 事务从第3点开始,这是您第一次在连接上执行命令时。

You can verify this using SQL Server Profiler. 您可以使用SQL Server Profiler进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM