using (TransactionScope trans = newTransactionScope())
{try{
InsertUserBase();//它插入不成功,自己回滚
UserInfos userInfo= newUserInfos
{
UserID= “1”,
RealName= “zzl”,
};
db.UserInfos.InsertOnSubmit(userInfo);
db.SubmitChanges();
trans.Complete();
}catch(Exception)
{//throw;
}finally{
trans.Dispose();
}
}#endregion
static boolInsertUserBase()
{boolflag;try{
UserBases userbase= newUserBases
{
UserID= “0005”,
Name= “zzl”,
CreateDate=DateTime.Now,
UpdateDate=DateTime.Now,
};
db.UserBases.InsertOnSubmit(userbase);
db.SubmitChanges();
flag= true;
}catch(Exception)
{throw;
}returnflag;
}
InsertUserBase()只要出现异常,程序将自己回滚
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, newTransactionOptions
{
IsolationLevel=System.Transactions.IsolationLevel.ReadCommitted,
Timeout= new TimeSpan(0, 10, 0)
}, TransactionScopeAsyncFlowOption.Enabled))
one or more errors occurred(The current TransactionScope is already complete)
如果你正在使用TransactionScope和async/await在一起,你真的应该升级到4.5.1 .NET马上。
一个TransactionScope包装异步代码需要指定TransactionScopeAsyncFlowOption.Enabled在其构造。
TransactionOptions option = new TransactionOptions() { Timeout = new TimeSpan(0, 10, 0), IsolationLevel = IsolationLevel.ReadCommitted };
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, option, TransactionScopeAsyncFlowOption.Enabled))
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/134594.html