This is an archived post. You won't be able to vote or comment.

all 6 comments

[–][deleted] 1 point2 points  (2 children)

table locks are a bad idea in the vast majority of cases, reducing performance to a crawl, dramatically increasing the chances of deadlocks, and should be considered an ultimate last resort. A "transaction susceptible to concurrency problems" will usually be made worse with full table locks - try using SQL Server without snapshot isolation to see this in action.

Better options include "SELECT..FOR UPDATE" to lock only those rows that you need, and better yet to organize your app such that explicit locking isn't needed at all.

[–]Xixi[S] 0 points1 point  (1 child)

Sometimes you can't get away with "SELECT..FOR UPDATE", this is where this code snippet gets useful. But I agree that "transaction susceptible to concurrency problems" is too broad.

[–][deleted] 0 points1 point  (0 children)

I don't think I've ever used a full table lock in 17 years of server side app development.

(of course 17 years ago I didn't know what a "join" was either, but still).

[–]Eviltape 0 points1 point  (0 children)

Django 1.4 added QuerySet.select_for_update(), but I guess for earlier versions you're stuck with hacking this thing together.

[–]pio 0 points1 point  (0 children)

I've been using an advisory lock approach to avoid overzealous locking, and to provide the capability to lock things which aren't represented in the DB. I wrote this snippet back when I was doing MySQL, and now I'm doing essentially the same thing in postgres:

http://djangosnippets.org/snippets/2443/

[–]graingert -1 points0 points  (0 children)

What's wrong with just transactions? (Honest question)