使用亚音速交易

The*_*iot 7 c# asp.net subsonic transactions web-applications

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

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

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

The*_*iot 15

答案给出@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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 10

就像是:

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
Run Code Online (Sandbox Code Playgroud)