How to sync between processes rather than threads

本文介绍了一种使用数据库记录来实现互斥锁的方法,适用于不同进程间的并发访问同步问题,通过创建或替换存储过程,确保数据访问的正确性和一致性。

# lock across process
e.g. Server handles request from different clients, there clients are essentially different processes, they could potentially write to the same piece
of data(e.g. same records in table), so we need to make sure these access well synchronized.

Since normal locking mechnism applies only to inter process(inter-threads), for normal lock from JDK won't work here.

In this case, we need to use some medium from outside, which could be database records or some file on our hard disk.

## Using database records to be mutual exclusive

CREATE OR REPLACE PROCEDURE BBDEV_DBO.Prcubblock(txtType IN CHAR, success OUT INT) IS
intRecCount INT;
BEGIN
     DELETE FROM UBBMSTLOCK WHERE dtlastmodified <= SYSDATE - 1/48;
     SELECT COUNT(*) INTO intRecCount FROM UBBMSTLOCK;
     IF (intRecCount = 0) THEN
         INSERT INTO UBBMSTLOCK VALUES (' ', 0, SYSDATE);
     END IF;
     IF (txtType = 'U') THEN
          UPDATE UBBMSTLOCK l
          SET l.txtlocktype = 'U', l.dtlastmodified = SYSDATE
          WHERE l.txtlocktype = ' ' OR l.txtlocktype = 'U';
          success := SQL%Rowcount;
     ELSIF txtType = 'S' THEN
          UPDATE UBBMSTLOCK l
          SET l.txtlocktype = 'S', l.dtlastmodified = SYSDATE
          WHERE l.txtlocktype = ' ' OR l.txtlocktype = 'S';
          success := SQL%Rowcount;
          IF (success > 0) THEN
             UPDATE UBBMSTLOCK l
             SET l.Intlockcount = l.Intlockcount + 1, l.dtlastmodified = SYSDATE
             WHERE l.txtlocktype = 'S';
          END IF;
     ELSE
          success := 0;
     END IF;
     COMMIT;
EXCEPTION
WHEN OTHERS THEN
     ROLLBACK;
     success := 0;
END;
/
View Code

 



转载于:https://www.cnblogs.com/glf2046/p/4740539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值