Forum: Ruby on Rails table lock

2d8b8b3782611eae032f194a900db4ef?d=identicon&s=25 Vetrivel Vaithilingam (vetrivel)
on 2009-03-02 06:57
   How to  lock a table in rails not row level lock .
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-02 10:25
(Received via mailing list)
vendor/rails/activerecord/lib/active_record/locking/pessimistic:
    # Pass <tt>:lock => true</tt> 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 <tt>:lock => 'some locking clause'</tt> 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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-02 10:43
(Received via mailing list)
On Mar 2, 9:24 am, MaD <mayer.domi...@gmail.com> wrote:
> vendor/rails/activerecord/lib/active_record/locking/pessimistic:
>     # Pass <tt>:lock => true</tt> to ActiveRecord::Base.find to obtain
> an exclusive

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

Fred
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-02 11:18
(Received via mailing list)
> That's a row lock :-) 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...
2d8b8b3782611eae032f194a900db4ef?d=identicon&s=25 Vetrivel Vaithilingam (vetrivel)
on 2009-03-02 11:42
Frederick Cheung wrote:
> On Mar 2, 9:24�am, MaD <mayer.domi...@gmail.com> wrote:
>> vendor/rails/activerecord/lib/active_record/locking/pessimistic:
>> � � # Pass <tt>:lock => true</tt> to ActiveRecord::Base.find to obtain
>> an exclusive
>
> That's a row lock :-) 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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-02 13:02
(Received via mailing list)
On 2 Mar 2009, at 10:42, Vetrivel Vetrivel 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
2d8b8b3782611eae032f194a900db4ef?d=identicon&s=25 Vetrivel Vaithilingam (vetrivel)
on 2009-03-02 15:59
Frederick Cheung wrote:
> On 2 Mar 2009, at 10:42, Vetrivel Vetrivel 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,
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-02 17:34
(Received via mailing list)
On Mar 2, 2:59 pm, Vetrivel Vetrivel <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
10f7e00c1c5c60e2173995eb16f4ffc7?d=identicon&s=25 pepe (Guest)
on 2009-03-03 04:20
(Received via mailing list)
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 Vetrivel <rails-mailing-l...@andreas-
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.