I inherited a system with a rather strange error that occurs, perhaps every 6 months, when an application suddenly loses traces of database data.
The system has redundancy with two servers that must run the same function at the same time. They both receive the same input for the function, and they both talk to the same postgres database, but the behavior on different machines is different from the others.
The executed function calls the database and checks if there is a line with the specified identifier specified by the input parameter, and if it is executed A()
, otherwiseB()
The problem is that one server is running A()
and the other B()
. I searched everywhere and no code is written to or deleted from this table. Therefore, for the whole reason, I think that they should execute the same code.
This is the code that is retrieved from the database:
@PersistenceContext(unitName = "backend-persistence")
private EntityManager em;
public Optional<OfferEntity> getOfferFromOfferId(final long offerId, final String countryAlias, final String langauageAlias) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<OfferEntity> cq = cb.createQuery(OfferEntity.class);
Root<OfferEntity> from = cq.from(OfferEntity.class);
cq.select(from);
cq.where(cb.and(cb.equal(from.get(OfferEntity_.offerId), offerId),
cb.equal(from.get(OfferEntity_.country), countryAlias),
cb.equal(from.get(OfferEntity_.language), langauageAlias)));
try {
return Optional.of(em.createQuery(cq).getSingleResult());
} catch (NoResultException nre) {
return Optional.empty();
}
}
And I get an empty version from one of the servers, but not the other.
So, I think like tl; dr, I will skip a NoResultException and in what specific situations can this be thrown? in addition, if there are no rows matching the query.