GoogleCloudPlatform / bank-of-anthos

Retail banking sample application showcasing Kubernetes and Google Cloud
https://cymbal-bank.fsi.cymbal.dev
Apache License 2.0
981 stars 580 forks source link

App fails to deploy if transaction database is empty #238

Closed djmailhot closed 4 years ago

djmailhot commented 4 years ago

The balance reader service is unable to correctly instantiate when there are no transactions in the database. It hits an issue with retrieving TransactionRepository.latestId() and getting a Null value.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'balanceReaderController' defined in URL [jar:file:/opt/monolith/ledgermonolith.jar!/BOOT-IN
F/classes!/anthos/samples/financedemo/ledgermonolith/BalanceReaderController.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanI
nstantiationException: Failed to instantiate [anthos.samples.financedemo.ledgermonolith.BalanceReaderController]: Constructor threw exception; nested exception is org.springframew
ork.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract long anthos.samples.financedemo.ledgermonolith.TransactionR
epository.latestId()
djmailhot commented 4 years ago

We can try updating TransactionRepository.java to return a default value when no transactions are found:

32     @Query("SELECT ISNULL(MAX(transactionId),'-1') FROM Transaction")
33     Long latestId();
djmailhot commented 4 years ago

This approach seems to work:

    /**
     * Returns the id of the latest transaction, or -1 if no transactions exist.
     */
    @Query(value = "SELECT COALESCE("
        + "(SELECT MAX(transaction_id) FROM transactions), "
        + "CAST ('-1' AS BIGINT))",
        nativeQuery = true)
    Long latestId();