2010-05-30 3 views
3
using (TransactionScope scope = new TransactionScope()) 
{ 
    int updatedRows1 = custPh.Update(cust.CustomerID, tempPh1, 0); 
    int updatedRows2 = custPh.Update(cust.CustomerID, tempPh2, 1); 
    int updatedRows3 = cust.Update(); 

    if (updatedRows1 > 0 && updatedRows2 > 0 && updatedRows3 > 0) 
    { 
     scope.Complete(); 
    } 
} 

위의 TransactionScope 코드가 올바르게 구성되어 있습니까? 이것은 내가 그것을 할 수있는만큼 간단하게하려고 노력하고 그래서 그것을 사용하여 처음입니다.질문 .NET의 TransactionScope 정보

+1

잘 사용하고 있습니다. 요구 사항에 따라 주변 트랜잭션에 참여하거나 참여하지 않도록 할 수 있습니다. TransactionOptions 매개 변수로이를 설정합니다. http://simpleverse.wordpress.com/2008/08/05/using-transactionscope-for-handling-transactions/ –

+0

@Mike. 올바른 것이므로 답을 만들어야합니다. – Steven

+1

실제로 나는 그 기사가 시대에 뒤진 Mike 일 것 같다고 생각합니다. TransactionScope는 더 이상 DTC를 감싸는 래퍼가 아니며 훨씬 더 유용합니다. http://stackoverflow.com/questions/1155941/transactionscope-has-it-gotten-better –

답변

8

잘 자물쇠가 채워져 있습니다.

하지만 당신이하고있는 일은 좋지 않습니다. 모든 테이블에 업데이트 된 행이 없으면 기본적으로 롤백을 수행하고 있습니다. 거래가 완료되었는지 또는 실패했는지 결코 알 수 없습니다. 오류로 인해 고통을 해결할 수 있습니다.

무엇인가 잘못되었을 경우 예외를 던집니다. 그것은 롤백으로 이어질 것입니다. 왜냐하면 scope.Complete()에 결코 도달하지 않기 때문입니다.

using (TransactionScope scope = new TransactionScope()) 
{ 
    int updatedRows1 = custPh.Update(cust.CustomerID, tempPh1, 0); 
    int updatedRows2 = custPh.Update(cust.CustomerID, tempPh2, 1); 
    int updatedRows3 = cust.Update(); 

    if (updatedRows1 == 0 || updatedRows2 == 0 || updatedRows3 == 0) 
     throw new Exception("Not all rows could be updated"); 

    scope.Complete(); 
} 
+0

+1을 사용하면 예외가 발생합니다! –

+0

굉장 ................. – peace