2010-01-26 4 views
2

데이터베이스 집약적 인 응용 프로그램의 핵심 요소 중 하나는 트랜잭션을 가능한 짧게 유지하는 것입니다.ADO.NET으로 작성된 db 트랜잭션은 실제로 언제 시작됩니까?

오늘은이 거래 실제로 시작할 것이다 때 궁금 : 단계 (1)에서

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; 
      } 
     } 
    } 

을, (2) 또는 (3)? 이상적으로 3 단계에 있어야합니다.

+0

좋은 질문입니다, 잊지 마세요 sqlTransaction.Rollback(); 실패 할 수도 ... –

답변

6

트랜잭션은 처음 연결을 시작할 때 지점 3에서 시작됩니다.

SQL Server 프로파일 러를 사용하여이를 확인할 수 있습니다.

관련 문제