Is there any way to do a table lock through the django database api?
I can't find information on it anywhere.
Hi all,
I'm using MySQL MyISAM tables, which means transactions don't work --
but they do support table locks. If not directly through the django
api, is there some hybrid of raw SQL and django models that I use to
achieve this?
I tried writing generic lock tables and unlock tables methods, as
such:
-----------------
def lock_tables(connection, args):
'''
Performs a table lock with the given connection, args is a
list of tuples
with (<table_name>, <lock_type["READ" |
"WRITE"]>)
**Remember to call unlock_tables at some point after this
call
'''
cursor = connection.cursor()
tbl_clause = [MySQLdb.escape_string(x[0] + " " + x[1]) for x in
args]
cursor.execute("LOCK TABLES %s" % MySQLdb.escape_string(',
'.join(tbl_clause)))
def unlock_tables(connection):
'''
Unlocks all table locks for the given
connection
'''
cursor = connection.cursor()
cursor.execute("UNLOCK TABLES")
-----------------
I used these around calls that read and modify the database -- I got
connection using "from django.db import connection" inline in the
method and the args were a write lock on the table I was editing with
read locks on its foreign keys.
This worked in the trivial case of one user in the django python
shell.
However, when I tried to load a page that tried to read the table -
while it was still write locked - the page was not returning (as I
would expect), so I unlocked the tables from within the python shell
and then I received an OperationalError suggesting that the browser
view had screwed up locks (even though there was nothing in the view
code that called the locking methods - maybe this was middleware?) --
"Table '<table_name>' was not locked with LOCK TABLES"
Is this approach fatally flawed -- does django.db import connection
return only one globally unique connection in django?
Does anyone have any ideas on how this may be possible... if not I
guess I'll move on and start using a transactional database like
innodb
Regards.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---