为什么我在程序中指定了TransactionScope的隔离级别ReadCommitted,事务中的表还是被锁定不能查询了?貌似设置成ReadUncommitted等别的也没用
TransactionOptions option = new TransactionOptions();
option.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, option))
{--这块更新的表被锁了。
}
TransactionOptions option = new TransactionOptions();
option.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, option))
{--这块更新的表被锁了。
}
一旦记录更新后,就会加上互斥锁(exclusive lock),其他人读取就会block住,直到事务提交/回滚。
这是如果要读取而不阻塞的话,要用 SELECT * FROM <table> WITH(NOLOCK)
或者在另外一个事务中读取,另外一个事务的级别要低于ReadUncommitted。在sql server中,执行下面的语句可以在表锁定时也能读数据:
alter database <youDatabaseName> set read_committed_snapshot on话说事务不就是应该越短越好吗?长时间事务处理可能会带来消耗资源、阻塞、死锁等各种问题,应尽可能避免。