Skip to content

Commit

Permalink
[Fix_#3405] Using merge in all cases (apache#1990)
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Feb 20, 2024
1 parent 72d2550 commit 104a3c8
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.kie.kogito.index.jpa.storage;

import java.util.ConcurrentModificationException;
import java.util.Map;
import java.util.function.Function;

Expand All @@ -27,8 +26,6 @@

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;

import jakarta.persistence.LockModeType;
import jakarta.persistence.PersistenceException;
import jakarta.transaction.Transactional;

import static java.util.stream.Collectors.toMap;
Expand All @@ -54,19 +51,7 @@ protected AbstractStorage(PanacheRepositoryBase<E, K> repository, Class<V> model
@Override
@Transactional
public V put(K key, V value) {
//Pessimistic lock is used to lock the row to handle concurrency with an exiting registry
E persistedEntity = repository.findById(key, LockModeType.PESSIMISTIC_WRITE);
E newEntity = mapToEntity.apply(value);
if (persistedEntity != null) {
repository.getEntityManager().merge(newEntity);
} else {
try {
//to handle concurrency in case of a new registry persist flush and throw an exception to allow retry on the caller side
repository.persistAndFlush(newEntity);
} catch (PersistenceException e) {
throw new ConcurrentModificationException(e);
}
}
repository.getEntityManager().merge(mapToEntity.apply(value));
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
*/
package org.kie.kogito.index.jpa.storage;

import java.util.Optional;

import org.kie.kogito.index.jpa.mapper.ProcessDefinitionEntityMapper;
import org.kie.kogito.index.jpa.model.ProcessDefinitionEntity;
import org.kie.kogito.index.jpa.model.ProcessDefinitionEntityRepository;
import org.kie.kogito.index.model.ProcessDefinition;
import org.kie.kogito.index.model.ProcessDefinitionKey;

import io.quarkus.hibernate.orm.panache.PanacheRepositoryBase;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.LockModeType;
import jakarta.transaction.Transactional;

@ApplicationScoped
Expand All @@ -42,7 +36,7 @@ protected ProcessDefinitionEntityStorage() {

@Inject
public ProcessDefinitionEntityStorage(ProcessDefinitionEntityRepository repository, ProcessDefinitionEntityMapper mapper) {
super(new RepositoryAdapter(repository), ProcessDefinition.class, ProcessDefinitionEntity.class, mapper::mapToModel, mapper::mapToEntity, e -> new ProcessDefinitionKey(e.getId(),
super(repository, ProcessDefinition.class, ProcessDefinitionEntity.class, mapper::mapToModel, mapper::mapToEntity, e -> new ProcessDefinitionKey(e.getId(),
e.getVersion()));
}

Expand All @@ -52,37 +46,4 @@ public boolean containsKey(ProcessDefinitionKey key) {
return getRepository().count("id = ?1 and version = ?2", key.getId(), key.getVersion()) == 1;
}

public static class RepositoryAdapter implements PanacheRepositoryBase<ProcessDefinitionEntity, ProcessDefinitionKey> {

ProcessDefinitionEntityRepository repository;

public RepositoryAdapter(ProcessDefinitionEntityRepository repository) {
this.repository = repository;
}

@Override
public boolean deleteById(ProcessDefinitionKey key) {
return repository.deleteById(key);
}

@Override
public Optional<ProcessDefinitionEntity> findByIdOptional(ProcessDefinitionKey key) {
return repository.findByIdOptional(key);
}

@Override
public ProcessDefinitionEntity findById(ProcessDefinitionKey s, LockModeType lockModeType) {
return repository.findById(s, lockModeType);
}

@Override
public void persist(ProcessDefinitionEntity entity) {
repository.persist(entity);
}

@Override
public EntityManager getEntityManager() {
return repository.getEntityManager();
}
}
}

0 comments on commit 104a3c8

Please sign in to comment.