Skip to content

Commit

Permalink
Add comments to explain _get_or_create_from_stripe_object logic
Browse files Browse the repository at this point in the history
  • Loading branch information
therefromhere committed Jun 23, 2019
1 parent fa8d8a5 commit 451fbc8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions djstripe/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,8 @@ def _get_or_create_from_stripe_object(
)

try:
# We wrap the `_create_from_stripe_object` in a transaction to avoid TransactionManagementError
# on subsequent queries in case of the IntegrityError catch below. See PR #903
with transaction.atomic():
return (
cls._create_from_stripe_object(
Expand All @@ -435,6 +437,10 @@ def _get_or_create_from_stripe_object(
True,
)
except IntegrityError:
# Handle the race condition that something else created the object after the `get`
# and before `_create_from_stripe_object`.
# This is common during webhook handling, since Stripe sends multiple webhook events simultaneously,
# each of which will cause recursive syncs. See issue #429
return cls.stripe_objects.get(id=id_), False

@classmethod
Expand Down

0 comments on commit 451fbc8

Please sign in to comment.