Table lock

How to lock a table in rails not row level lock .

vendor/rails/activerecord/lib/active_record/locking/pessimistic:
# Pass :lock => true to ActiveRecord::Base.find to obtain
an exclusive
# lock on the selected rows:
# # select * from accounts where id=1 for update
# Account.find(1, :lock => true)
#
# Pass :lock => ‘some locking clause’ to give a database-
specific locking clause
# of your own such as ‘LOCK IN SHARE MODE’ or ‘FOR UPDATE NOWAIT’.
#
# Example:
# Account.transaction do
# # select * from accounts where name = ‘shugo’ limit 1 for
update
# shugo = Account.find(:first, :conditions => “name =
‘shugo’”, :lock => true)
# yuko = Account.find(:first, :conditions => “name =
‘yuko’”, :lock => true)
# shugo.balance -= 100
# shugo.save!
# yuko.balance += 100
# yuko.save!
# end
#
# You can also use ActiveRecord::Base#lock! method to lock one
record by id.
# This may be better if you don’t need to lock every row. Example:
# Account.transaction do
# # select * from accounts where …
# accounts = Account.find(:all, :conditions => …)
# account1 = accounts.detect { |account| … }
# account2 = accounts.detect { |account| … }
# # select * from accounts where id=? for update
# account1.lock!
# account2.lock!
# account1.balance -= 100
# account1.save!
# account2.balance += 100
# account2.save!
# end

On Mar 2, 9:24 am, MaD [email protected] wrote:

vendor/rails/activerecord/lib/active_record/locking/pessimistic:
# Pass :lock => true to ActiveRecord::Base.find to obtain
an exclusive

That’s a row lock :slight_smile: If you want a table lock you need to run the
appropriate sql statement for your database by hand.

Fred

That’s a row lock :slight_smile: If you want a table lock you need to run the
appropriate sql statement for your database by hand.

Fred

oh, i’m sorry. it seems i’m still a little bit sleepy and didn’t read
the question correctly. i thought you were looking for a row lock…

On 2 Mar 2009, at 10:42, Vetrivel V. wrote:

Fred

Please give me some examples to accomplish the table level lock.

That depends on your database. ActiveRecord::Base.connection.execute
allows you to execute arbitrary sql statements.

Fred

Frederick C. wrote:

On Mar 2, 9:24�am, MaD [email protected] wrote:

vendor/rails/activerecord/lib/active_record/locking/pessimistic:
� � # Pass :lock => true to ActiveRecord::Base.find to obtain
an exclusive

That’s a row lock :slight_smile: If you want a table lock you need to run the
appropriate sql statement for your database by hand.

Fred

Please give me some examples to accomplish the table level lock.

Frederick C. wrote:

On 2 Mar 2009, at 10:42, Vetrivel V. wrote:

Fred

Please give me some examples to accomplish the table level lock.

That depends on your database. ActiveRecord::Base.connection.execute
allows you to execute arbitrary sql statements.

Fred

I have a two tables a and b . At first I am fetching one records from
a.Then I have to use table lock to lock that table. Then I will do some
operatins,Then I insert a record in b table.Then I will remove the
lock.Untill such times other rails application should not read the data
from a table.How to do this using ActiveRecord::Base class , Say Sql
Query to do that.

thanks,

This is a MySQL page where it talks about table locking:

http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

Pepe

On Mar 2, 9:59 am, Vetrivel V. <rails-mailing-l…@andreas-

On Mar 2, 2:59 pm, Vetrivel V. <rails-mailing-l…@andreas-
s.net> wrote:

I have a two tables a and b . At first I am fetching one records from
a.Then I have to use table lock to lock that table. Then I will do some
operatins,Then I insert a record in b table.Then I will remove the
lock.Untill such times other rails application should not read the data
from a table.How to do this using ActiveRecord::Base class , Say Sql
Query to do that.

Like i said before, depends on your database (and in general table
locks are a bad idea and will hurt you as your number of users
increases)

Fred