When NoSuchResult Can Be Thrown

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.

+4
source share
1 answer

You can use getSingleResult () when you are sure that you will get exactly one result. In all other cases, you should use getResultList ()

From javax.persistence.Query API document getSingleResult ():

java.lang.Object getSingleResult()

Execute a SELECT query that returns a single untyped result.

Returns:
the result

Throws:
NoResultException - if there is no result
NonUniqueResultException - if more than one result
IllegalStateException - if called for a Java Persistence query language UPDATE or DELETE statement
QueryTimeoutException - if the query execution exceeds the query timeout value set and only the statement is rolled back
TransactionRequiredException - if a lock mode has been set and there is no transaction
PessimisticLockException - if pessimistic locking fails and the transaction is rolled back
LockTimeoutException - if pessimistic locking fails and only the statement is rolled back
PersistenceException - if the query execution exceeds the query timeout value set and the transaction is rolled back
+1

Source: https://habr.com/ru/post/1692715/


All Articles