2

I have situation where I can't update column at database. The problem is that only method setIsPurchased is executed and the flag is_purchase at SQL table changed to true, but the next line with setPurchasedDate which has to set current time is not executed. The response from database is Mono<Void> type.

fun purchase(uuid: UUID, request: PurchaseInternalRequest) =
        extraProductTransactionService.findTransactionByUuid(uuid)
            .doOnNext { it.validateNotPurchased() }
            .flatMap { purchaseViaSupplier(it, request) }
            .flatMap { extraProductTransactionService.setIsPurchased(uuid) }
            .flatMap { extraProductTransactionService.setPurchasedDate(uuid) }
            .onErrorMap(Exception::class.java) { Exception(it.status) }
            .then()

Queries for those two method are simple and looks like:

For setIsPurchased:

update extra_product_transactions SET is_purchased = 1 where uuid = :uuid

For setPurchasedDate:

update extra_product_transactions SET purchased_date = CURRENT_TIMESTAMP where uuid = :uuid

1 Answers1

0

A Mono<Void> by essence never emits an element onNext(). Having said that, .flatMap { extraProductTransactionService.setPurchasedDate(uuid) } is never invoked.

I assume you are not interested in the return values of the set methods so you can do something like:

fun purchase(uuid: UUID, request: PurchaseInternalRequest) =
    extraProductTransactionService.findTransactionByUuid(uuid)
        .doOnNext { it.validateNotPurchased() }
        .flatMap { purchaseViaSupplier(it, request) }
        .flatMap { 
          extraProductTransactionService.setIsPurchased(uuid).subscribe()
          extraProductTransactionService.setPurchasedDate(uuid)
        }
        .onErrorMap(Exception::class.java) { Exception(it.status) }
        .then()
João Dias
  • 16,277
  • 6
  • 33
  • 45
  • Thank you for your approach, I decied to change it to one query. I mean ```setIsPurchasedAndPurchasedAt```. For your information, I tried change return type to something else than ```Mono``` (Mono) the problem do not disappear – blindDriver Sep 13 '21 at 09:22
  • I am confused. Is your problem fixed now? – João Dias Sep 13 '21 at 11:51
  • Yes, now my code looks like ```fun purchase(uuid: UUID, request: PurchaseInternalRequest) = extraProductTransactionService.findTransactionByUuid(uuid) .doOnNext { it.validateNotPurchased() } .flatMap { purchaseViaSupplier(it, request) } .flatMap { extraProductTransactionService.setIsPurchasedAndPurchasedAt(uuid) } .onErrorMap(Exception::class.java) { Exception(it.status) } .then()``` where ```setIsPurchasedAndPurchasedAt``` is query to update both values. Best regards – blindDriver Sep 14 '21 at 07:38
  • PS Sorry for style of message but here i can't edit it – blindDriver Sep 14 '21 at 07:40
  • Great! If my answer helped you upvote it or even accept is as the answer to your question so that others may benefit from it and understand easily what a solution could look like. Thanks! – João Dias Sep 14 '21 at 09:07