Skip to Content
Author's profile photo Former Member

ASE 15的Readpast锁定技术

       “Readpast” 锁定技术(需要ASE 15.0及以上版本) 通过在select、readtext、update、delete和 writetext等语句中指定”readpast”选项,可以让这些语句在执行时自动跳过遇到的所有不兼容锁,而不会阻塞、终止或生成错误消息。也就是说,对于这些指定了readpast选项的语句,ASE只读取或修改数据行锁定表上未锁定的记录行,或数据页锁定表上的未锁定页上的记录行;而被锁定的记录行会被ASE自动跳过,而不会阻塞。Readpast 锁定只能在事务隔离级别1或2中使用。
       某些采用了MVCC技术实现隔离级别1的数据库(比如,Oracle)产品中查询和更新是不会相互阻塞的。ASE的readpast锁定与它们有一些相似,即不会在锁定行上阻塞,但是,ASE中带有”Readpast”选项的语句在查询或更新的记录集合中并不会包含那些加有不兼容锁的记录(因为这些记录会被ASE自动跳过),这与MVCC技术的数据库不同。采用MVCC技术的数据库在查询时会得到全部满足条件的记录,并且不会阻塞。
       “Readpast”锁定技术,主要用于数据库表中存放的是队列记录的情况。在这种情况下,许多任务可能访问这个数据库表来处理排队的记录。例如,这些记录可能代表排队的客户或客户订单。给定的任务可能并不关心队列中的某个特定成员,但可处理任何满足相应处理标准的队列成员。
       为了说明”Readpast”锁定技术,我们看一个示例。假设示例用到的表accounts有如下记录:
          actno         balance           
          ———      ———–
        1111111      1020.00
         1111112      1000.00
         1111113      900.00
        1111114      500.00
        1111115      3200.00
      假设事物运行在非链式模式、隔离级别1下,表accounts采用”datarows”锁定机制。下面让我们看一个简单的例子,假设事物TA先执行了update语句,如下:
      begin tran
         ……
         update account set balance = balance+10 where actno=’1111111′
         ……
      commit
      当事物TA执行完update语句后,假设TB事物执行select语句,如下:
      begin tran
         ……
         select * from accounts readpast
         ……
      commit

     如果事物TB中的select语句不加readpast选项,那么由于事物TA在执行update语句时会对actno=’1111111’这条记录加”排他行锁(Ex_row)”,因此这个查询会阻塞。如果加上 readpast选项,那么select语句不会阻塞,会返回除了actno=’1111111之外的所有记录,即返回4条记录: 
       actno                  balance           
       ———             ———–
       1111112            1000.00
       1111113             900.00
       1111114             500.00
       1111115            3200.00

     Readpast 锁定的其它一些示例参考:
      (1) 若要跳过含有排它锁的所有行,使用以下命令:
          select * from titles readpast

    (2) 若要只更新没有被其它会话锁定的行,请使用以下命令:
         update titles
        set price = price * 1.1
         from titles readpast

   (3) 对 titles 表而不是 authors 或 titleauthor 表使用 readpast 锁定:  
        select *
         from titles readpast, authors, titleauthor
         where titles.title_id = titleauthor.title_id
           and authors.au_id = titleauthor.au_id

   (4) 只删除没有在 stores 表中锁定的行,但允许扫描在 authors 表上阻塞:
         delete stores  
        from stores readpast, authors
         where stores.city = authors.city

   (5) 更新accounts表中没有加锁记录中的头一行记录
        begin tran
           update top 1 account
           set balance = balance +1
           from account readpast
           select top 1 actno, balance from account
       commit

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.