EAS使用网络互斥锁,查询互斥锁

互斥锁id可以查询表 T_BAS_DataUpdateMutex

select * from T_BAS_DataUpdateMutex

没有用户参数时可使用以下

复制代码
//增加网络互斥 
        IMutexServiceControl iMutex = MutexServiceControlFactory.getLocalInstance(ctx);
        String billId = null;
       try {
           IRowSet rowSet = DbUtil.executeQuery(ctx, sql);        
            while(rowSet.next())
            {
                 boolean mutexLock = false;
                //设置为关闭状态
                 billId = rowSet.getString("FID");//单据id
                //增加互斥锁
                 if(iMutex.requestObjIDForUpdate(billId)){
                     mutexLock =true;
                 }else{
                     continue;
                 }
                try {
                    String  rst= _pushJZTERP(ctx,new ObjectUuidPK(billId));
                } finally{
                    //释放互斥锁
                     if(mutexLock){
                         iMutex.releaseObjIDForUpdate(billId);
                     }
                }
            }
复制代码

当有用户参数时,可用如下

IMutexServiceControl mutex = MutexServiceControlFactory.getRemoteInstance();
 UserInfo user = null; // 请求锁有用户
 String billId = null; // 要锁定/解锁的单据Id
 // 请求锁定
 mutex.requestObjIDForUpdate(billId, user.getString("id"));
 // 解除锁
 mutex.releaseObjIDForUpdate(billId);

 可以查询如下方式判断是否获取互斥锁

IMutexServiceControl mutexServiceControlDao = MutexServiceControlFactory.getLocalInstance(ctx);
boolean request = mutexServiceControlDao.requestObjIDForUpdate(saleOrderId);//EAS产品互斥锁 
if (!request) {

  throw new EASBizException(new NumericExceptionSubItem("","数据占用中不允许操作:"));
}

 另外可以进行批量的id加锁,具体如下:

复制代码
//对占用票据加锁,加锁失败抛异常
  //给预占用票据上锁,防止这批票据被其他人使用
  IMutexServiceControl iMutex = MutexServiceControlFactory.getLocalInstance(ctx);
    ArrayList<String> imutexIdList = new ArrayList<String>();
    try{
        Map mutexRequest = iMutex.batchRequestObjIDForUpdate(billIdList);
        ArrayList<String> usedIds = new ArrayList<String>();
        for (int index = 0; index < billIdList.size(); index++) {
            Boolean boolRequest = (Boolean) mutexRequest.get(billIdList.get(index));
            if (!boolRequest) {
                usedIds.add(billIdList.get(index));
                continue;
            }
            imutexIdList.add(billIdList.get(index));// 添加获取成功锁的票据id,用于后续释放占用
        }
        if (!EmptyUtil.isEmpty(usedIds)){
            throw new Exception("预占用的票据id:" + usedIds + " 已被锁定,请稍后再试!");
        }
    )catch(Exception e){
        
    }finally{
        //释放本次成功锁定的票据id
        if (!EmptyUtil.isEmpty(imutexIdList)){
            iMutex.batchReleaseObjIDForUpdate(imutexIdList);
        } 
    }
复制代码

 

posted @   凉了记忆  阅读(92)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示