Skip to content

Conversation

@seeker25
Copy link
Collaborator

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the sbc-pay license (Apache 2.0).

@seeker25 seeker25 requested review from Jxio and ochiu as code owners December 30, 2025 21:18
@seeker25 seeker25 changed the title Add db.session begin to for update Concurrency issue Dec 31, 2025
if identifier:
return cls.query.filter_by(id=identifier).with_for_update().one_or_none()
query = cls.query.filter_by(id=identifier).with_for_update()
query = query.populate_existing()
Copy link
Collaborator Author

@seeker25 seeker25 Dec 31, 2025

Choose a reason for hiding this comment

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

If you have an existing fetch of eg payment_account.find_by_id, this will refresh the row, if you don't do this you may have old data which causes a row conflict, we didn't see this in the versioned unit test because it only grabbed the data once

account_id=invoice.payment_account_id,
created_invoice_id=invoice.id,
).flush()
).save()
Copy link
Collaborator Author

@seeker25 seeker25 Dec 31, 2025

Choose a reason for hiding this comment

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

This should be save, because the credit is already created in CMS

I was running into weird issues with flush to do with deadlocks it would trigger a large query it seemed like, this should be save anyways

case _:
# I don't believe there are CC (DirectPay flow not DirectSale) refunds, wouldn't want a credit back
raise NotImplementedError(f"Payment method {invoice.payment_method_code} not implemented for credits.")
payment_account.save()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This should be save, because the credit is already created in CMS

Comment on lines +34 to +37
"watchdog": {
"level": "WARNING",
"handlers": ["structured"],
"propagate": false
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

was getting weird spam calls

pad_account_credit = payment_account.pad_credit or 0
payment_account.pad_credit = 0 if pad_account_credit < invoice.total else pad_account_credit - invoice.total
payment_account.flush()
payment_account.save()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

invoice creation commits after this anyways, should be ok

@seeker25 seeker25 merged commit ac59c10 into bcgov:main Dec 31, 2025
17 checks passed
@seeker25 seeker25 changed the title Concurrency issue History row conflict concurrency issue Dec 31, 2025
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