What is the granularity of the named locks? Seems like table level.
Think of these locks as mutexes in threaded code: they don't mean anything on their own, but they simply guard against the rest of your code. Contrived semi-pseudocode example:
def add_item_to_cart(order, item):
try:
lock = acquire_named_lock('order-{}'.format(order.id), timeout=10)
order.add_item(item)
order.recalculate_total()
order.save()
lock.release()
except LockTimeoutError:
raise OrderUpdateError('Timed out trying to add item to order.')
def checkout(order):
try:
lock = acquire_named_lock('order-{}'.format(order.id), timeout=10)
order.pay()
order.generate_and_email_invoice()
order.print_shipping_label()
order.save()
lock.release()
except LockTimeoutError:
raise OrderUpdateError('Timed out trying to check out.')
Note that the two functions will never step on each others' toes because the first thing they do is acquire a lock.Essentially these are mutexes identified by strings that are managed by your RDBMS. That's all.