在TransactionScope中,Serializable隔离级别某些情况不起作用
1.在TransactionScope中,Serializable隔离级别某些情况不起作用
2.测试代码
使用可重复读隔离级别没什么问题,但是用默认即序列化时出现问题。
即如果先执行scoreRepository.Add(userId, scoreCount);新增数据,后进行总和增加时事物没起作用。
执行顺序反过来就有作用
public static void AddScore(object data)
{
var dataTest = (TestData)data;
try
{
//using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted }))
//using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.RepeatableRead }))
//using (var tran = new TransactionScope())
using (var tran = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.Serializable }))
{
var userId = 3;
var scoreCount = dataTest.Random.Next(1,100);
dataTest.Message = dataTest.Message + "分数:"+ scoreCount;
var userInfoRepository = new UserInfoRepository();
var scoreRepository = new ScoreRepository();
scoreRepository.Add(userId, scoreCount);
userInfoRepository.AddSocre(userId, scoreCount);
dataTest.Message = dataTest.Message + "成功";
tran.Complete();
}
}
catch (Exception ex)
{
dataTest.Message = dataTest.Message + "异常" + ex.Message;
}
finally
{
dataTest.mutipleThreadResetEvent.SetOne();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论