Calculation returns random results

Hi,

I have a calculate balance function in the middleware service, for some reason it some times returns random results which are incorrect, even when I refresh multiple times it keeps returning different results for the same account. The only way it returns the proper result I had to restart the full application.

Here is the code

@Override
public BigDecimal calculateBalance(Account account) {
    com.haulmont.cuba.core.Transaction tx = persistence.getTransaction();

    EntityManager em = persistence.getEntityManager();

    TypedQuery<Transaction> query = em.createQuery(
            "select o from cps$Transaction o where o.account.id = :id", Transaction.class);
    query.setParameter("id", account.getId());
    List<Transaction> transactions = query.getResultList();


    query = em.createQuery(
            "select o from cps$Transaction o where o.receiver.id = :id", Transaction.class);
    query.setParameter("id", account.getId());
    transactions.addAll(query.getResultList());

    BigDecimal balance = BigDecimal.ZERO;

    for (Transaction t: transactions) {
        if(t.getType() == TransactionType.DEPOSIT){
            balance = balance.add(t.getAmount()).subtract(t.getFees().multiply(t.getFees_exchange_rate()));
        }
        if(t.getType() == TransactionType.WITHDRAW){
            balance = balance.subtract(t.getAmount()).subtract(t.getFees().multiply(t.getFees_exchange_rate()));
        }
        if(t.getType() == TransactionType.TRANSFER){
            if(t.getAccount().getId().equals(account.getId())) {
                balance = balance.subtract(t.getAmount().multiply(t.getExchange_rate()))
                        .subtract(t.getFees().multiply(t.getFees_exchange_rate()));
            }
            else if(t.getReceiver().getId().equals(account.getId())) {
                balance = balance.add(t.getAmount());
            }
        }
        if(t.getType() == TransactionType.ORDER){
            balance = balance.subtract(t.getAmount());
        }

        if(t.getType() == TransactionType.SELL){
            balance = balance.add(t.getAmount());
        }

        if(t.getType() == TransactionType.REFUND){
            balance = balance.subtract(t.getAmount());
        }
    }


    // get all receipts to calculate balance after alteration
    TypedQuery<Receipt> query2 = em.createQuery(
            "select o from cps$Receipt o where o.account.id = :id", Receipt.class);
    query2.setViewName("receipt-view");
    query2.setParameter("id", account.getId());
    List<Receipt> receipts = query2.getResultList();

    for(Receipt r : receipts){
        // subtract full total to show debt
        balance = balance.add(r.getTotal());
        for(Transaction t :  r.getTransactions()){
            // add balance to settle debt
            if(t.getType() != TransactionType.REFUND) {
                balance = balance.subtract(t.getAmount());
            }else{
                balance = balance.add(t.getAmount());
            }
        }
    }

    return balance;
}

I sometimes receive this error for some reason

com.haulmont.cuba.core.global.RemoteException:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.cuba24): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
      Position: 163
    Error Code: 0
    Call: SELECT ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, POSITION_, SECTION_, UPDATE_TS, UPDATED_BY, VERSION, FACILITY_ID FROM CPS_FACILITY_DETAIL WHERE ((ID IN ()) AND (0=0))
    Query: ReadAllQuery(name="facility" referenceClass=FacilityDetail sql="SELECT ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, POSITION_, SECTION_, UPDATE_TS, UPDATED_BY, VERSION, FACILITY_ID FROM CPS_FACILITY_DETAIL WHERE ((ID IN ?) AND (0=0))")
    ---
    org.eclipse.persistence.exceptions.DatabaseException: 
    Internal Exception: org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
      Position: 163
    Error Code: 0
    Call: SELECT ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, POSITION_, SECTION_, UPDATE_TS, UPDATED_BY, VERSION, FACILITY_ID FROM CPS_FACILITY_DETAIL WHERE ((ID IN ()) AND (0=0))
    Query: ReadAllQuery(name="facility" referenceClass=FacilityDetail sql="SELECT ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, POSITION_, SECTION_, UPDATE_TS, UPDATED_BY, VERSION, FACILITY_ID FROM CPS_FACILITY_DETAIL WHERE ((ID IN ?) AND (0=0))")
    ---
    org.postgresql.util.PSQLException: ERROR: syntax error at or near ")"
    ---

I’m not sure why you have random results, but your code has a problem: you open a transaction and does not end it. If the transaction is really started in this method, the database connections pool will be quickly consumed and the application will stop responding.

1 Like

Thanks will check if that helps how about the error code that I receive all over the application in some of the request that I call including the calculateBalance function.

Provide the log content around the error output.