【发布时间】:2018-09-27 02:16:04
【问题描述】:
我正在使用这个基本结构:
using(IDataContext ctx = DataContext.Instance())
{
try{
ctx.BeginTransaction();
ctx.Commit();
}
catch(Exception)
{
ctx.RollbackTransaction();
throw;
}
}
我想做的是嵌套事务,以便我可以使用函数式编程进行构建。
非常简化的版本:
public void DoSomething(){
using(IDataContext ctx = DataContext.Instance())
{
try{
ctx.BeginTransaction();
// make a change to the data
ctx.Commit();
}
catch(Exception)
{
ctx.RollbackTransaction();
throw;
}
}
}
public void CallingFunction(){
using(IDataContext ctx = DataContext.Instance())
{
try{
ctx.BeginTransaction();
//do some stuff
DoSomething();
//do some other stuff
ctx.Commit();
}
catch(Exception)
{
ctx.RollbackTransaction();
throw;
}
}
}
所以我希望能够拥有多个调用 DoSomething() 的“CallingFunctions”,但如果在 DoSomething 之后的 CallingFunction 中的代码中抛出异常,那么我希望 DoSomething 也回滚。
DoSomething 可能与 CallingFunction 属于同一类,也可能属于另一个类。
这当然是可能的,但我一直无法找到答案。 感谢您的帮助。
检查我的代码后,我意识到它使用的是来自 DotNetNuke.Data 命名空间的 DataContext。也许 DNN 专家可以提供帮助?
【问题讨论】:
标签: c# transactions nested dotnetnuke datacontext