How to lock a table in rails not row level lock . -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 2, 9:24 am, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> vendor/rails/activerecord/lib/active_record/locking/pessimistic: > # Pass <tt>:lock => true</tt> to ActiveRecord::Base.find to obtain > an exclusiveThat''s a row lock :-) If you want a table lock you need to run the appropriate sql statement for your database by hand. Fred> # 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> That''s a row lock :-) If you want a table lock you need to run the > appropriate sql statement for your database by hand. > > Fredoh, 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... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Mar 2, 9:24�am, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > FredPlease give me some examples to accomplish the table level lock. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2 Mar 2009, at 10:42, Vetrivel Vetrivel wrote:> > Frederick Cheung wrote: >> On Mar 2, 9:24�am, MaD <mayer.domi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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.That depends on your database. ActiveRecord::Base.connection.execute allows you to execute arbitrary sql statements. Fred> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. > > FredI 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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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> > thanks, > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> 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, > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---