Table locking with ActiveRecord

Hi,

what is the preferred way to lock a table for writes using
ActiveRecord? I have a custom id field (i.e. not auto_increment) which
uniqueness I would like to protect in the following way:
begin transaction and lock table for writes
select latest id from table
insert new row with id = latest_id+1
commit and unlock

As far as I can see this functionality isn’t provided by the current
ActiveRecord version and I am a bit unsure how to implement it in a
“Rails way”. I am using MySQL 5 wiht InnoDB as a storage engine.

Cheers,
Phil

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Phil wrote:

As far as I can see this functionality isn’t provided by the current
ActiveRecord version and I am a bit unsure how to implement it in a
“Rails way”. I am using MySQL 5 wiht InnoDB as a storage engine.

You could easily get away with

Model.connection.execute( “LOCK TABLES #{Model.table_name} WRITE” )

Model.connection.execute( “UNLOCK TABLES” )

I think I am going to add this to my ActiveRecord Extensions as…

Model.lock :write=>true, :read=>true, etc…
Model.unlock

Thoughts on API ?

Zach
http://blogs.mktec.com/zdennis/pages/ActiveRecord_Extensions
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE+wbQMyx0fW1d8G0RAswqAJ9iF+QWaCVasTKUCQQ0c3P5yHG3AACfZGxT
5FXM8D3Cu6UbYDET6Vv0t+s=
=r/VE
-----END PGP SIGNATURE-----