On Feb 15, 6:12 pm, Koen Bok <[email protected]> wrote:
> I'm trying to design a small webstore and running into some issues.
>
> I have an order that has multiple lines. If any of those lines change
> in quantity my stock entity should reflect that, so they should be
> wrapped in a transaction. But as an order can have multiple lines
> (referring to multiple stocks) and can has only one parent that won't
> work.
>
> So I will create two entities for the order:
>
> Order
> OrderLine (parent: stock)
>
> But now I have a problem with the order/orderline group, as they need
> to be transactional too. So I was thinking, get rid of the order
> object and only have orderlines, but that doesn't solve anything
> because the orderlines will not be transactional with other orderlines
> they relate to.
>
> So the only thing I can still think of is making a store entity and
> make it the parent of orders and stocks like this:
>
> Store
> Stock (parent: store)
> Order (parent: store)
>
> But this will give me horrible performance as every stock or order can
> only be updated max 5 times a second (for being in the same entity
> group). Anyone got a better idea?
First off, does your order really need to be in a transaction with
order lines?
If you can't avoid that portion, then you may want to consider a
slightly more complex protocol for stock updates. Consider a new
entity type, a StockReservation.
When you create an orderline, before actually creating it, decide on a
key for it. Use a random number or UUID or something to ensure it
won't collide.
Now, in a transaction:
* Reduce the available stock.
* Create a StockReservation, with parent = the Stock entity, an
arbitrary key, containing the amount you reduced the stock by, a copy
of the orderline's key, and the current time stamp.
Then in a seperate transaction:
* Create your orderline with parent = the Order entity
* Update your Order entity
Then in a final transaction:
* Delete the StockReservation
If the orderline update fails before the actual orderline creation,
you could have some stale reservations; so periodically go through any
StockReservations older than a minute or so, and reverse them - that
is:
* (not in a transaction) Select a StockReservation
* Check if its OrderLine exists. If so, delete the StockReservation
and stop.
* Enter a transaction and:
* Verify the StockReservation still exists
* Add its quantity back to the Stock entity
* Delete the Stock Reservation
There's one more failure mode that must be addressed; if the
StockReservation is not deleted, and later the OrderLine is deleted,
you could end up with too much stock. This can be addressed by making
sure that anything that deletes an orderline first searches for a
StockReservation associated with that OrderLine, and deletes it if
found.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google App Engine" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/google-appengine?hl=en
-~----------~----~----~----~------~----~------~--~---