Browse Source

fixed concurrency exception when deleting relation

pull/5513/head
Dima Landiak 5 years ago
committed by Andrew Shvayka
parent
commit
541110b344
  1. 6
      dao/src/main/java/org/thingsboard/server/dao/sql/relation/JpaRelationDao.java
  2. 20
      dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java

6
dao/src/main/java/org/thingsboard/server/dao/sql/relation/JpaRelationDao.java

@ -157,7 +157,11 @@ public class JpaRelationDao extends JpaAbstractDaoListeningExecutorService imple
private boolean deleteRelationIfExists(RelationCompositeKey key) {
boolean relationExistsBeforeDelete = relationRepository.existsById(key);
if (relationExistsBeforeDelete) {
relationRepository.deleteById(key);
try {
relationRepository.deleteById(key);
} catch (ConcurrencyFailureException e) {
log.debug("[{}] Concurrency exception while deleting relation", key, e);
}
}
return relationExistsBeforeDelete;
}

20
dao/src/test/java/org/thingsboard/server/dao/service/BaseRelationServiceTest.java

@ -16,6 +16,8 @@
package org.thingsboard.server.dao.service;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -31,6 +33,7 @@ import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
import org.thingsboard.server.dao.exception.DataValidationException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
@ -84,6 +87,23 @@ public abstract class BaseRelationServiceTest extends AbstractServiceTest {
Assert.assertTrue(relationService.deleteRelationAsync(SYSTEM_TENANT_ID, childId, subChildId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON).get());
}
@Test
public void testDeleteRelationConcurrently() throws ExecutionException, InterruptedException {
AssetId parentId = new AssetId(Uuids.timeBased());
AssetId childId = new AssetId(Uuids.timeBased());
EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
saveRelation(relationA);
List<ListenableFuture<Boolean>> futures = new ArrayList<>();
for (int i = 0; i < 2; i++) {
futures.add(relationService.deleteRelationAsync(SYSTEM_TENANT_ID, relationA));
}
List<Boolean> results = Futures.allAsList(futures).get();
Assert.assertTrue(results.contains(true));
}
@Test
public void testDeleteEntityRelations() throws ExecutionException, InterruptedException {
AssetId parentId = new AssetId(Uuids.timeBased());

Loading…
Cancel
Save