Illegal pop() with non-matching JdbcValuesSourceProcessingState

509 Views Asked by At

I am using Quarkus reactive with hibernate reactive with a postgresql database to build a non-blocking web application.

The following code should return the availability of all vehicles stored in the database for a specific planned ride. The availability is derived from the tables vehicles (weather the vehicles is defective), reservations (whether there is already an active reservation for that vehicle) and planned rides (whether there is a different ride planned and overlaps the period of the requested ride).

I came up with the following solution:

@GET
@Path("/availabilities")
public Uni<List<VehicleAvailability>> vehicleAvailabilitiesForRide(@QueryParam("rideId") String rideId) {

    return PlannedRide.<PlannedRide>findById(rideId).map(Unchecked.function(ride -> {
        if (ride == null) {
            throw new NotFoundException();
        }
        return ride;
    })).flatMap(ride -> {

        Uni<List<VehicleReservation>> reservationsUni = VehicleReservation.findReservationsInPeriod(ride.getStart(), ride.getEnd());
        Uni<List<PlannedRide>> plannedRidesUni = PlannedRide.findRidesInPeriod(ride.getStart(), ride.getEnd());

        return Uni.combine().all().unis(reservationsUni, plannedRidesUni).asTuple().flatMap(tuple -> {

            Uni<List<Vehicle>> vehiclesUni = Vehicle.findAll().list();
            List<VehicleReservation> reservations = tuple.getItem1();
            List<PlannedRide> plannedRides = tuple.getItem2();

            return vehiclesUni.map(vehicles -> vehicles.stream().map(vehicle -> {
                if (vehicle.isDefective()) {
                    return new VehicleAvailability(vehicle, Availability.DEFECTIVE);
                }
                //check reservation
                for (VehicleReservation reservation : reservations) {
                    if (Objects.equals(reservation.getVehicle().getId(), vehicle.getId())) {
                        //vehicle is reserved in period
                        if (Objects.equals(reservation.getPlannedRide().getId(), ride.getId())) {
                            //reserved for current ride
                            return new VehicleAvailability(vehicle, Availability.RESERVED);
                        } else {
                            return new VehicleAvailability(vehicle, Availability.NOT_AVAILABLE);
                        }
                    }
                }
                if (plannedRides.stream().anyMatch(iterRide -> !Objects.equals(ride.getId(), iterRide.getId()))) {
                    return new VehicleAvailability(vehicle, Availability.POTENTIALLY_AVAILABLE);
                }
                return new VehicleAvailability(vehicle, Availability.AVAILABLE);
            }));
        });
    }).map(Stream::toList);
}

First, I have to find the period in which the requested ride will take place. Then, I fetch the reservations and the rides for that period using Uni#combine(). After that, for every vehicle, I return a new DTO with the appropriate state.

Unfortunately, I get an Illegal pop() with non-matching JdbcValuesSourceProcessingState exception and I do not know how to resolve it.

After some googeling, I found that a session can not be used concurrently and I created a new Session for every database access using Munity.SessionFactory#openSession(), #withSession() and #withTransaction. However the problem persisted :(. However, when I do not use the Uni#combine method, and just map the result of either the plannedRidesUni or the reservationsUni, the code works fine, but I really need both of them for the different states.

0

There are 0 best solutions below