[3.3.2] Fix bug while deleting devices with relations to one another (#5310)

* Fix bug while deleting devices with relations to one another

* Change log level from error to debug

* Made try catch block more precise
This commit is contained in:
Mykhailo Kratiuk 2021-10-11 13:20:16 +03:00 committed by GitHub
parent 69e2f0c115
commit 0026857e27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching; import org.springframework.cache.annotation.Caching;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
@ -201,6 +202,7 @@ public class BaseRelationService implements RelationService {
} }
relationDao.deleteOutboundRelations(tenantId, entityId); relationDao.deleteOutboundRelations(tenantId, entityId);
} }
@Override @Override
@ -262,10 +264,13 @@ public class BaseRelationService implements RelationService {
boolean delete(TenantId tenantId, Cache cache, EntityRelation relation, boolean deleteFromDb) { boolean delete(TenantId tenantId, Cache cache, EntityRelation relation, boolean deleteFromDb) {
cacheEviction(relation, cache); cacheEviction(relation, cache);
if (deleteFromDb) { if (deleteFromDb) {
return relationDao.deleteRelation(tenantId, relation); try {
} else { return relationDao.deleteRelation(tenantId, relation);
return false; } catch (ConcurrencyFailureException e) {
log.debug("Concurrency exception while deleting relations [{}]", relation, e);
}
} }
return false;
} }
private void cacheEviction(EntityRelation relation, Cache cache) { private void cacheEviction(EntityRelation relation, Cache cache) {

View File

@ -18,6 +18,7 @@ package org.thingsboard.server.dao.sql.relation;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.EntityType;
@ -163,11 +164,16 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
@Override @Override
public boolean deleteOutboundRelations(TenantId tenantId, EntityId entity) { public boolean deleteOutboundRelations(TenantId tenantId, EntityId entity) {
boolean relationExistsBeforeDelete = relationRepository boolean relationExistsBeforeDelete = false;
.findAllByFromIdAndFromType(entity.getId(), entity.getEntityType().name()) try {
.size() > 0; relationExistsBeforeDelete = relationRepository
if (relationExistsBeforeDelete) { .findAllByFromIdAndFromType(entity.getId(), entity.getEntityType().name())
relationRepository.deleteByFromIdAndFromType(entity.getId(), entity.getEntityType().name()); .size() > 0;
if (relationExistsBeforeDelete) {
relationRepository.deleteByFromIdAndFromType(entity.getId(), entity.getEntityType().name());
}
} catch (ConcurrencyFailureException e) {
log.debug("Concurrency exception while deleting relations [{}]", entity, e);
} }
return relationExistsBeforeDelete; return relationExistsBeforeDelete;
} }