搞了个定时任务,报错,差点以为是定时任务不支持事务呢,生产日志打的缺,不如IDEA打的多,坑啊。不是的。是其他异常了
Caused by: java.lang.UnsupportedOperationException
看发生位置,是在Iterator 里
while (it.hasNext()) {
String str = (String) it.next();
if (str.endsWith(“0000”)) {
// this
it.remove();
}
}
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public void completePeoplesInOrIds()
List<OaSceneEntity> sceneList=oaSceneService.selectList(new EntityWrapper<OaSceneEntity>().eq("scene_type",2));
if(!CollectionUtils.isEmpty(sceneList)){
for (OaSceneEntity oaSceneEntity : sceneList) {
//查询设备的部门人员和已配的人员增减,进行相应增删
addOrSubPeoplesByOrIds(oaSceneEntity);
}
}
}
private void addOrSubPeoplesByOrIds(OaSceneEntity oaSceneEntity ){
String orIds=oaSceneEntity.getOrIds();
if(StringUtils.isNotBlank(orIds)){
//异常的原因分析:Arrays.asList()返回由[原始数组]支持的列表并且[您对列表所做的更改也会反映在您传入的数组中],坑啊,不是具体的List/平时玩耍理解上的真正的List,而后续的iterator会抛UnsupportedOperationException(数组不支持remove)
List<String> orIdList=Arrays.asList(orIds.split(","));
List<String> hrIdList = null;
if (!CollectionUtils.isEmpty(orIdList)) {
hrIdList = getHrIdsFromOrIds(orIdList);
}
处理:for each循环删asList而来的List的元素时会异常。采用Iterator
public List<String> getHrIdsFromOrIds(List<String> orIdListOrigin) {
//大部门id后面有0000标志。只可能是选全选时出现了这个
//this
List<String> orIdList = new ArrayList<>(orIdListOrigin);
Iterator<String> it = orIdList.iterator();
while (it.hasNext()) {
String str = (String) it.next();
if (str.endsWith("0000")) {
it.remove();
}
}
iterator.remove() java.lang.UnsupportedOperationException异常原因及解决