记一次数据库事务锁

最近在做项目的时候碰到一个问题,事务锁。

TransactionOptions tos = new TransactionOptions();

tos.IsolationLevel = IsolationLevel.RepeatableRead; //行锁 只会锁住当前操作的那一行数据,当前表的其他数据不受影响。 (已验证)

//IsolationLevel.Serializable;  //表锁 当前操作会将整张表锁住,只有该事务提交后,才可以操作该表的数据 (已验证)

//"select * from tableName where id = 1 for update"; //查询的时候使用 for update,当前数据行仍然可以更新,只是在该事物还未提交时,其他事务再操作这张表就会锁行,需要等待该事务提交完毕才可以操作和提交(未验证)

tos.Timeout = new TimeSpan(0, 2, 0);

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tos))
{
 
  //业务处理
  InitStockQty(item);
  ts.Complete();
}

猜你喜欢

转载自www.cnblogs.com/opts/p/9138511.html
今日推荐