How to lock tables in mysql with SqlAlchemy?

5,921 views
Skip to first unread message

Ting Zhou

unread,
Apr 24, 2008, 12:35:47 PM4/24/08
to sqlal...@googlegroups.com
Dear All,

I would like to lock a table like "LOCK TABLES table_name" in mysql
command. How can I do that with SqlAlchemy.
I have defined a class

|//|//|class Pointer(Entity):
using_options(tablename='Pointer',autosetup=True)
id=Field(MSInteger,primary_key=True)

|////|I need to lock table |////|'Pointer'.|//

jason kirtland

unread,
Apr 24, 2008, 5:51:55 PM4/24/08
to sqlal...@googlegroups.com

You can lock the tables by executing the SQL directly. I'm not sure
what that looks like in Elixir, but in plain SA it'd be something like:

conn = engine.connect()
conn.execute("LOCK TABLES Pointer WRITE")
... do stuff with conn
conn.execute("UNLOCK TABLES")

Ting Zhou

unread,
Apr 25, 2008, 2:50:45 AM4/25/08
to sqlal...@googlegroups.com
jason kirtland 写道:
Thank you. This is what I am doing at the moment. I am hoping it can be
done more pythonically.
Best wishes
Ting

Michael Bayer

unread,
Apr 25, 2008, 11:51:37 AM4/25/08
to sqlal...@googlegroups.com

On Apr 25, 2008, at 2:50 AM, Ting Zhou wrote:

> Thank you. This is what I am doing at the moment. I am hoping it can
> be
> done more pythonically.

locking tables is a coarse grained action that can lead to deadlocks.
Finer grained row locking is available using SELECT...FOR UPDATE which
the ORM supports directly using
query.with_lockmode(<"read"|"update">), and select() supports using
the "for_update" keyword argument: select([cols],
for_update=<True|"read">)

Reply all
Reply to author
Forward
0 new messages