2
數據庫密集型應用程序的關鍵之一是儘可能縮短事務處理時間。ADO.NET中寫入的db事務何時實際開始?
今天我想知道當這個交易將實際開始:
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中。
不錯的問題,不要忘記,sqlTransaction.Rollback();也可能失敗... – 2011-07-08 20:45:55