背景
在业务产品中,存在 Oracle 移植过来的 SQL 语句。它们使用 for update of column 对字段上锁。lightdb 23.4 版本对此语法糖进行了支持。
lightdb 支持语法 for update of column
在 lightdb 23.4 以后 oracle 兼容模式中 for update of column 将会锁表或者锁行,与 for update 功能保持一致。兼容 oracle 的 for update of column 写法不报错,其中 for update of 为关键字 column 为列名(语法糖实现暂时不判断列名的正确性)。该功能在执行 select 语句时上锁查询结果。被锁住的行或者表将无法执行update语句修改数据。具体如下示例
示例一锁表
在session1中创建表
create table my_column(id int , name varchar);
insert into my_column(id, name) values(1, 'mumu');
insert into my_column(id, name) values(2, 'anni');
在session1启动事务并上锁
begin;
select * from my_column , my_column as s for update of id;
在session2执行update
update my_column set name = 'dema' where id =1;
等待session1提交
在session1提交事务
commit;
在session2中
session1提交后 session2 获取锁后即执行update成功
示例二锁行
在session1中启动事务并上锁
begin;
select * from my_column where id = 1 for update of id;
在session2 中执行update
update my_column set name = 'akali' where id = 2;
执行成功
update my_column set name = 'xiong' where id = 1;
等待session1提交
在session1提交事务
commit;
在session2中
session1提交后 session2 获取锁后即执行update成功