使用亚音速交易

在我的Web应用程序中,我要继续审核用户操作。 因此,每当用户执行操作时,我都会更新执行操作的对象,并保留该操作的审计跟踪。

现在如果我先修改对象然后更新审计跟踪但审计跟踪失败那么呢?

显然我需要回滚对修改过的对象的更改。 我可以在简单的应用程序中使用Sql-Transactions,但我使用Subsonic与db通信。 我怎么能处理这种情况?

就像是:

Using ts As New System.Transactions.TransactionScope() Using sharedConnectionScope As New SubSonic.SharedDbConnectionScope() ' Do your individual saves here ' If all OK ts.Complete() End Using End Using 

@Kevinw给出的答案是完全可以的。 我发布这个就像他对C#代码的回答一样。 我没有使用注释,因为它不会格式化代码:)我也使用try / catch来了解事务是否应该完成或回滚。

 using (System.Transactions.TransactionScope ts = new TransactionScope()) { using (SharedDbConnectionScope scs = new SharedDbConnectionScope()) { try { //do your stuff like saving multiple objects etc. here //everything should be completed nicely before you reach this //line if not throw exception and don't reach to line below ts.Complete(); } catch (Exception ex) { //ts.Dispose(); //Don't need this as using will take care of it. //Do stuff with exception or throw it to caller } } } 

不。 如果我把SharedDbConnectionScope外面,那么在ts.Complete()之前,数据库中的变化是ts.Complete() 。 将其置于内部阻塞服务器直到操作完成。