在我的网络应用程序中,我必须对用户操作进行审核。因此,每当用户执行操作时,我都会更新执行操作的对象并保留该操作的审核跟踪。
现在,如果我先修改对象,然后更新审计跟踪,但审计跟踪失败了,那么怎么办?
显然我需要回滚对已修改对象的更改。我可以在简单的应用程序中使用 Sql-Transactions,但我使用 Subsonic 与数据库对话。我该如何处理这种情况?
The answer https://stackoverflow.com/questions/910863/using-transactions-with-subsonic/911069#911069给出的@Kevinw https://stackoverflow.com/users/108853/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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)