2012-04-21 2 views
0

이 코드에서는 .commit() 뒤에 SomeFunction()을 트랜잭션의 일부로 간주합니까? 무언가가 날아간다면 롤백 되겠습니까? 동적 레코드가 삽입 된 후 추가 처리가 필요하고 모든 것을 하나의 큰 덩어리로 처리하는 것을 선호합니다.Sqlclient 트랜잭션 및 .Commit()

command.Transaction = transaction 
Try 
    command.CommandText = _ 
    "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')" 
    command.ExecuteNonQuery() 
    transaction.Commit() 
    'do a function call here 
    SomeFunction() 
Catch ex As Exception 
    transaction.Rollback() 
End Try 

답변

0

아니오 Somefunction()라고하는 트랜잭션이 이미 완료되었으므로 롤백 할 수 없습니다.

SomeFunction이 예외를 throw하면 catch 블록은 롤백 할 활성 트랜잭션이 없으므로 transaction.Rollback() 메서드에서 예외를 throw합니다.

Somefunction() 호출을 Exception 블록 아래로 이동하고 가능한 경우 다른 try catch 블록에 넣어야합니다.

command.Transaction = transaction 
Try 
    command.CommandText = _ 
    "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')" 
    command.ExecuteNonQuery() 
    transaction.Commit() 

Catch ex As Exception 
    transaction.Rollback() 
End Try 

    'do a function call here 
    SomeFunction() 
+0

예, 나는 생각합니다. 내가 피하고 싶었던 것처럼 모든 것을 sp에 넣을 것입니다. 확인해 주셔서 감사합니다! – Somejerk