记一次Activiti5.22发生的用户-流程关联错乱问题

在查询用户参与的流程时,发现ACT_RU_IDENTITYLINK和ACT_HI_IDENTITYLINK存储了无关用户ID,原因在于ThreadLocal中用户ID值在多线程环境下发生互串。解决方法包括修改源码或在流程启动后清空用户ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述

查询用户参与的流程时(query.involvedUser(uid)😉,返回了完全不相关的流程信息,发现ACT_RU_IDENTITYLINK和ACT_HI_IDENTITYLINK都存储了和流程无关的用户ID,角色类型都是参与者participant,于是一步步梳理。

源码追踪

首先由任务完成的complete方法定位到
TaskEntity类的complete方法

if (Authentication.getAuthenticatedUserId() != null && processInstanceId != null) {
  getProcessInstance().involveUser(Authentication.getAuthenticatedUserId(), IdentityLinkType.PARTICIPANT);
}

原来在这里进行了participant的插入

public IdentityLinkEntity involveUser(String userId, String type) {
  for (IdentityLinkEntity identityLink : getIdentityLinks()) {
    if (identityLink.isUser() && identityLink.getUserId().equals(userId)) {
      return identityLink;
    }
  }
  return addIdentityLink(userId, null, type);
}

再跟踪Authentication.getAuthenticatedUserId()方法,定位到

public abstract class Authentication {

  static ThreadLocal<String> authenticatedUserIdThreadLocal = new ThreadLocal<String>();
  
  public static void setAuthenticatedUserId(String authenticatedUserId) {
    authenticatedUserIdThreadLocal.set(authenticatedUserId);
  }

  public static String getAuthenticatedUserId() {
    return authenticatedUserIdThreadLocal.get();
  }
}

于是真相大白,虽然说ThreadLocal会为不同线程保存变量,然而毕竟索引的key是线程ID,而工程里使用了线程池,于是导致ThreadLocal中的值互串~~~

解决方法

1、改写源码,在使用后调用ThreadLocal的remove方法,成本比较高
2、在流程启动后,调用setAuthenticatedUserId(null)

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值