Skip to content

Commit

Permalink
Use entityManager's flush method to commit the allocation block state
Browse files Browse the repository at this point in the history
  • Loading branch information
sundarvenkata authored and sundarvenkata-EBI committed Mar 23, 2020
1 parent f77f0a9 commit ac26c16
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.entities.ContiguousIdBlock;
import uk.ac.ebi.ampt2d.commons.accession.persistence.jpa.monotonic.repositories.ContiguousIdBlockRepository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -34,6 +36,9 @@ public class ContiguousIdBlockService {

private Map<String, BlockParameters> categoryBlockInitializations;

@PersistenceContext
EntityManager entityManager;

public ContiguousIdBlockService(ContiguousIdBlockRepository repository, Map<String, BlockParameters>
categoryBlockInitializations) {
this.repository = repository;
Expand All @@ -49,16 +54,20 @@ public void save(Iterable<ContiguousIdBlock> blocks) {
public ContiguousIdBlock reserveNewBlock(String categoryId, String instanceId) {
ContiguousIdBlock lastBlock = repository.findFirstByCategoryIdOrderByLastValueDesc(categoryId);
BlockParameters blockParameters = getBlockParameters(categoryId);
ContiguousIdBlock reservedBlock = null;
if (lastBlock != null) {
return repository.save(lastBlock.nextBlock(instanceId, blockParameters.getBlockSize(),
reservedBlock = repository.save(lastBlock.nextBlock(instanceId, blockParameters.getBlockSize(),
blockParameters.getNextBlockInterval(),
blockParameters.getBlockStartValue()));
entityManager.flush();
} else {
ContiguousIdBlock newBlock = new ContiguousIdBlock(categoryId, instanceId,
blockParameters.getBlockStartValue(),
blockParameters.getBlockSize());
return repository.save(newBlock);
reservedBlock = repository.save(newBlock);
entityManager.flush();
}
return reservedBlock;
}

public BlockParameters getBlockParameters(String categoryId) {
Expand Down

0 comments on commit ac26c16

Please sign in to comment.