Concurrent command handling for same Aggregate hangs up

Hi, need some advice.

I have a billing app that handles payment commands. Whenever I receive multiple commands for the same aggregate, it fails to persist data to the database because both threads freeze. I posted a sample of code to here https://github.com/ryessekeyev/billing-sandbox. There is /api/payments-test endpoint which simulates the issue. I think the issue is the incorrect way of handling payment because I send other commands in the callback method on success. Need advice on how to fix it.

I attached a log file that shows the process.

billing-sanbox.log (38.3 KB)

Hi Rustem,

the repository uses locking to prevent multiple threads from accessing an Aggregate at the same time. Obviously, locks are also placed on database rows, in case Sagas or other non-event-sourced data is used.
The callbacks are invoked prior to releasing these locks. Normally, Transactions would have committed at this point, helping prevent database level deadlocks. However, from your code, it seems you are doing the dispatching asynchronously and in a self-managed transaction. This may be the cause of the deadlock occurring.

Instead, consider letting Axon manage the transaction. There is no need for command-dispatching to happen in the scope of a transaction. Only the handling needs to be. Axon will auto-configure a transaction manager on your command bus.

If you want async processing, we generally recommend using an AsyncCommandBus (or better, the AxonServer command bus, as it is also distributed) instead of doing async dispatching.

Hope this helps.
Cheers,