2012-04-21 16 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引发任何异常,catch块仍会在transaction.Rollback()方法中抛出异常,因为没有活动事务要回滚。

您应该将您的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 2012-04-22 01:23:18