0

I'm using Quarkus Reactive and I'm trying to insert data to DB if duplicate record is not found on DB, here's my line of code

@ApplicationScoped
public class MovieRepositories implements IMovieRepositories {
   @Inject
   MovieMapper mapper;

   @Override
   public Uni<Object> create(MovieEntity entity) {
      final var data = mapper.toEntity(entity);
      final var dataMovie = MovieEntity
            .find("name=?1 and deleted_at is null", entity.getName());
    return dataMovie.firstResult().onItem().ifNotNull()
          .failWith(new ValidationException("Movie already exist"))
          .call(c -> MovieEntity.persist(data))
          .chain(d -> Uni.createFrom().item(data.getId()));
   }

however, this code terminates after failure on this line of code

         .failWith(new ValidationException("Movie already exist"))

and persist is never executed.

How to make this code insert data if no duplicate record is found on?

Thanks in advance

  • if what you meant by reproducer is a step by step to reproduce this issue, I don't have it. just to clarify what I'm working with here, I'm trying to make an insert that was sent from an API to a message broker, here's a repo to execute that insert. I've tried many possibilities, including just brutally insert without any checking. it doesn't work. So there's a consumer that should call a services that will call this function in repo. Seems something is missing that causes this insert to fail. Maybe something about db session. – Anton Wijaya Dec 25 '22 at 06:03
  • without seeing the whole picture it is hard to guess where the problem is. that is why I asked for a reproducer, and one more good thing about reproducer, while you are preparing a reproducer you can find out what is missing :) – ozkanpakdil Dec 25 '22 at 10:17
  • Thanks for your suggestions, I finally got my code to work by adding return value to method that receive incoming message. This is caused by uncomitted transactions that makes it looks like data is not inserted. – Anton Wijaya Dec 27 '22 at 16:36

1 Answers1

1

Solved by adding a return value to incoming message method that is calling this insert. This happens due to uncommited transaction to mongodb. By simply adding return commited the transaction to mongodb