Skip to content

fix(wallet_transaction): Ensure consistency when creating wallet transaction #3895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2025

Conversation

vincent-pochet
Copy link
Collaborator

Context

An issue has been identifier on the POST /v1/wallet_transactions used to create a transaction on a wallet.

In some situation if the same wallet is being refreshed in a separate process (job, parallel call to the endpoint), an ActiveRecord::StaleObjectError can be raised when running an update on the model. The main reason is that the wallet model is using an optimistic locking approach to avoid inconsistencies.

The error is raised and it leads to an HTTP 500 response error returned by the API. An other problem appears here: the process to create the wallet transaction is not transactional, meaning that a wallet transaction might have been persisted in the database without impacting the wallet balance...

Description

This PR:

  • Wrap the logic in a single database transaction making sure nothing is persisted if something goes wrong in the process
  • Adds an auto retry logic when an ActiveRecord::StaleObjectError is raised

@vincent-pochet vincent-pochet force-pushed the fix-wallet-transaction-create branch from 6ffc5b1 to 5540344 Compare July 1, 2025 13:31
Copy link
Collaborator

@rsempe rsempe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@vincent-pochet vincent-pochet merged commit 0fad050 into main Jul 1, 2025
14 checks passed
@vincent-pochet vincent-pochet deleted the fix-wallet-transaction-create branch July 1, 2025 14:58
vincent-pochet added a commit that referenced this pull request Jul 3, 2025
## Context
This PR fixes a regression introduced by
#3895

The wallet transaction processing was performed in a single database
transaction. I the process a `BillPaidCreditJob` is enqueued. Since it's
now in the DB transaction, it leads in some situation to an
`ActiveJob::DeserializationError`.

## Description

This PR fixes the situation by enqueuing the job after the the commit of
the DB transaction
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
…saction (#3895)

## Context

An issue has been identifier on the `POST /v1/wallet_transactions` used
to create a transaction on a wallet.

In some situation if the same wallet is being refreshed in a separate
process (job, parallel call to the endpoint), an
`ActiveRecord::StaleObjectError` can be raised when running an update on
the model. The main reason is that the wallet model is using an
optimistic locking approach to avoid inconsistencies.

The error is raised and it leads to an HTTP 500 response error returned
by the API. An other problem appears here: the process to create the
wallet transaction is not transactional, meaning that a wallet
transaction might have been persisted in the database without impacting
the wallet balance...

## Description

This PR:
- Wrap the logic in a single database transaction making sure nothing is
persisted if something goes wrong in the process
- Adds an auto retry logic when an `ActiveRecord::StaleObjectError` is
raised
diegocharles pushed a commit that referenced this pull request Jul 11, 2025
## Context
This PR fixes a regression introduced by
#3895

The wallet transaction processing was performed in a single database
transaction. I the process a `BillPaidCreditJob` is enqueued. Since it's
now in the DB transaction, it leads in some situation to an
`ActiveJob::DeserializationError`.

## Description

This PR fixes the situation by enqueuing the job after the the commit of
the DB transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants