用 ADO.NET 编写的数据库事务何时真正开始?
数据库密集型应用程序中的关键之一是使事务尽可能短。
今天我想知道这笔交易何时真正开始:
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;
}
}
}
在步骤(1)、(2)或(3)中?理想情况下应该在步骤 3 中。
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)? Ideally it should be in step 3.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事务从点 3 开始,即您第一次在连接上执行命令时。
您可以使用 SQL Server Profiler 来验证这一点。
The transaction starts at point 3, the first time you exectue a command on the connection.
You can verify this using SQL Server Profiler.