大家好,又见面了,我是你们的朋友全栈君。
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))
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161929.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...