Hi All,

I tried writing some code to do table locking on a MySQL MyISAM
database.  I used the code below, which seemed to work in basic
examples, but the code would complain of accessing a non-locked table
whenever I made a more complex query such as
objects.filter(<foreignkeyname>__<value-of-that-foreign-key>) saying
that the table <foreignkeyname>__<value-of-that-foreign-key> wasn't
locked with "LOCK TABLES", even though I had locked the table for the
foreign key.  Furthermore, it failed to lock if I tried to reference
the table using the syntax of the form of the complex query.

Has anyone had success running table locks?

The code:  (note: I was getting connection using: from django.db
import connection)


 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
    '''
    if not len(args):
        raise Exception("Must supply args to lock table!")
    cursor = connection.cursor()
    try:
        tbl_clause = [MySQLdb.escape_string(x[0] + " " + x[1]) for x
in args]
    except:
        raise Exception("Args was improperly constructed")
    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")


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to