Have you ever changed your password, then forgot what you changed it to? No, of course you haven't. But I have, and on more than one occasion! Have you ever revoked your own authority to an object? It seems to me I've done that, too. When I do such things, I lock myself out of some resource. Recently, I was asked to work on a program that had locked itself out of a database record. I've seen one program lock another program out of a record many times, but I can't ever remember a program locking itself out of a record. Anyway, there's a lesson in this experience we can all benefit from.

Here's the situation. An RPG program accessed one physical file through three access paths--the physical file itself and two logical files that were built over the physical file. The program screeched to a halt when a record that had been read and locked for update through one access path was requested to be read for update through another access path. The system generated error message CPF5032 (Record 2 member CUSMASLC already locked to this job.)

I wrote a short demo program to illustrate this situation.

*** CusMas is a physical file keyed on company number & customer number.
FCusMas    uf   e           k disk    rename(CusMasMA: CusMas1)  
F                                     infds(infds1)   
F
*** CusMasLC is a logical file keyed on customer name.
FCusMasLC  uf   e           k disk    rename(CusMasRC: CusMas2)  
F                                     infds(infds2)

D SearchComp      s              2p 0                            
D SearchCus       s              8p 0                            
                           
D infds1          ds                                             
D Stat1             *status                                      
D infds2          ds                                             
D Stat2             *status                                      
                                                                
C     Search        klist                                        
C                   kfld                    SearchComp           
C                   kfld                    SearchCus            
C                                                                
C                   eval      SearchComp = 1                     
C                   eval      SearchCus = 302                    
C                                                                
C                   eval      *inlr = *on                        
C                                                                
C     Search        chain     CusMas1
C                   if        %found and CusCl ='27'
C                   eval      Terrn = 5            
C                   update    CusMas1              
C                   endif                          
C     
C* imagine a lot of calculation specs here
C                                            
C     CusNm         chain     CusMas2              
C                   if        %found               
C                   eval      DCode = 3            
C                   update    CusMas2              
C                   endif                          
C                                                  
C                   return

CUSMAS is a physical file containing customer information. CUSMASLC is a logical file that accesses CUSMAS by customer name. The first chain succeeds, but the IF that follows the chain fails because customer 302 in company 1 does not have a class code of 27. The UPDATE does not happen, which means that customer 302's record remains locked.

Imagine a lot of calculations taking place, until finally the second chain attempts to retrieve the same record by customer name. The record exists, of course, but it is locked. After the time-out period, the system sends CPF5032. By the way, if another program tried to access the locked record, it would error out with message RNQ1218 (Unable to allocate a record in file CUSMAS (R C G D F).)

It's not hard to see the proper solution, which is to unlock the record if the condition fails.

C     Search        chain     CusMas1
C                   if        %found and CusCl ='27'
C                   eval      Terrn = 5            
C                   update    CusMas1              
C                   else
C                   unlock    CusMas
C                   endif

(I wonder: "If the solution is obvious and so easy to implement, why didn't the programmer code the unlock in the first place?")

Those of you who have had your morning coffee are wondering: "What if the chain fails? There's nothing to unlock." And the answer is that the unlock doesn't blow up. Suppose the update is buried within two or more levels of nested logic.

C     Search        chain     CusMas1              
C                   if        %found and CusCl ='27'
C     RepKey        chain     RepMas               
C                   if        %found and Terr = 'W'
C                   eval      Terrn = 5            
C                   update    CusMas1              
C                   endif                          
C                   endif                          
C                   unlock    CusMas

Rather than code an unlock operation for each level, one unlock suffices for all.

Anyway, I try hard to develop and adhere to good programming practice. Here's a rule of thumb that's worth keeping in mind: If you embed an update or delete operation within a condition, be sure to release the lock if the condition doesn't pan out.

转载于:https://www.cnblogs.com/lane_yang/archive/2011/11/04/2235955.html

Release That Record Lock!相关推荐

  1. Mysql 死锁过程及案例详解之记录锁与间隔锁Record Lock Gap Lock

    记录锁Record Lock与间隔锁GAP Lock 记录锁Record Lock 记录锁Record Locks又称为行锁,它同时包含索引和间隔锁.记录锁可以是共享锁也可能是排他锁.可以通过perf ...

  2. InnoDB锁机制之Gap Lock、Next-Key Lock、Record Lock解析

    MySQL InnoDB支持三种行锁定方式: l   行锁(Record Lock):锁直接加在索引记录上面,锁住的是key. l   间隙锁(Gap Lock):锁定索引记录间隙,确保索引记录的间隙 ...

  3. MySQL笔记-InnoDB中Record Lock与Gap Lock

    锁:用于在多个事务访问同一个对象时根据这些操作访问同一对象的先后次序给事务排序. 不同数据库的锁实现: InnoDB:行级锁: Oracle:行级锁: MyISAM:表级锁: Microsoft SQ ...

  4. linux I/O-记录锁(record lock)

    记录锁(record lock)也称字节范围锁.文件范围锁.文件段锁,是一种在文件的某个字节.某个区域进行加锁的机制,记录锁总是和进程.文件相关.本篇博客介绍的是建议性记录锁. 1 记录锁的函数原型: ...

  5. Mysql INNODB引擎行锁的3种算法 Record Lock Next-Key Lock Grap Lock

    Mysql INNODB引擎行锁的3种算法 InnoDB存储引擎有3种行锁的算法,其分别是: □ Record Lock:单个行记录上的锁 Record Lock总是会去锁住索引记录,如果InnoDB ...

  6. mysql记录锁(record lock),间隙锁(gap lock),Next-key锁(Next-key lock)亲测

    行锁 记录锁(record lock) 这是一个索引记录锁,它是建立在索引记录上的锁(主键和唯一索引都算),很多时候,锁定一条数据,由于无索引,往往会导致整个表被锁住,建立合适的索引可以防止扫描整个表 ...

  7. mysql不同情况下加锁类型实验 record lock 和 gap lock

    首先确定RR级别下mysql的加锁规则(实验环境 mysql 8.0): 加锁的基本单位是 next-key lock.是一个前闭后开的区间,也是record lock 和 gap lock 的组合. ...

  8. mysql主键查询gap锁失效,mysql记录锁(record lock),间隙锁(gap lock),Next-key锁(Next-key lock)...

    1. 什么是幻读? 幻读是在可重复读的事务隔离级别下会出现的一种问题,简单来说,可重复读保证了当前事务不会读取到其他事务已提交的 UPDATE 操作.但同时,也会导致当前事务无法感知到来自其他事务中的 ...

  9. python 测试multiprocessing多进程

    文章目录 测试1 multiprocessing函数的调用 测试2 pipe 测试queue 测试1 multiprocessing函数的调用 # -*- coding: utf-8 -*- &quo ...

  10. [解析]多线程加锁Lock调用python2

    没有加锁的代码如下 from atexit import register from random import randrange from threading import Thread, cur ...

最新文章

  1. ios超级签名_ios超级签名何以固若金汤?原因在这里
  2. 学校SOLARIS ORACLE很好的电子教材
  3. 【C 语言】内存四区原理 ( 内存四区建立流程 )
  4. java 禁止使用多线程_Java多线程(四)-线程状态的转换 - Java 技术驿站-Java 技术驿站...
  5. 快速pow算法c语言_嵌入式必知基础算法(二)
  6. CentOS安装使用.netcore极简教程(免费提供学习服务器)
  7. 随机抽样java_实现随机抽样【随机数生成问题】
  8. WPF利用HelixToolKit后台导入3D模型
  9. 正交法设计测试用例时可以使用的工具allpairs---生成正交表
  10. c语言unicode编码转ascii码,编码转换(ASCII和Unicode、Unicode和中文相互转换)
  11. Python制作简单的学生成绩管理系统
  12. matlab 画表格函数,excel表格数据曲线图模拟函数-如何在excel中绘制函数图像?
  13. 实习僧——数据分析岗招聘信息爬取 源代码
  14. 【论文翻译】Multi-modal Knowledge Graphs for Recommender Systems
  15. 云虚拟主机bch 和 云服务器bcc,云虚拟主机bch 和 云服务器bcc
  16. 面试系统设计_如何进行系统设计面试
  17. 项目实战-----产品经理要做什么?
  18. win10下启动.bat文件闪退问题
  19. 推荐系统的评价指标笔记(NDCG、MAP、AUC、HR、MRR)
  20. 【LeeCode】赛题02:Python解答大衍数列题目

热门文章

  1. Maya vray 3S皮肤材质球设置与材质节点连接
  2. 含泪整理最优质花朵 鲜花 花卉VRay材质球素材,你想要的这里都有
  3. 微软官方dllcache恢复的批处理
  4. 最新苹果商务管理ABM注册及使用
  5. Improved Variational Inference with Inverse Autoregressive Flow
  6. solr使用 备忘录 草稿
  7. 转载 关于12360系统的讨论
  8. triplets 、triplet Loss和 hard triplets
  9. 【数据分析报告】携程客户分析与流失预测
  10. C语言中格式输出二进制的两种方法