Forum: Ruby on Rails How to lock and unlock table in ruby on rails?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D81c1821bb5fd0bb179a331175751908?d=identicon&s=25 Venkat Eee (venkat_ruby)
on 2009-05-06 10:03
Hello all,

I written the below code in common controller. I will pass the
model_name and it has to lock and unlock the table.
Please help me to correct this code...



def lock_table(model_name)
    locked_table = model_name.find(:all, :lock => true)
  return locked_table;
end



Thanks,
059ed46172a087063ce26250e44c8627?d=identicon&s=25 Fernando Perez (fernando)
on 2009-05-06 11:41
> I written the below code in common controller. I will pass the
> model_name and it has to lock and unlock the table.

That's not how it works. Behind the scenes, it will generate an SQL
statement: SELECT FOR UPDATE, if wrapped in a transaction, then it will
create a lock and then release it when transaction ends.
D81c1821bb5fd0bb179a331175751908?d=identicon&s=25 Venkat Eee (venkat_ruby)
on 2009-05-06 12:54
Fernando Perez wrote:
>> I written the below code in common controller. I will pass the
>> model_name and it has to lock and unlock the table.
>
> That's not how it works. Behind the scenes, it will generate an SQL
> statement: SELECT FOR UPDATE, if wrapped in a transaction, then it will
> create a lock and then release it when transaction ends.


I'm new to ruby. Can u give me some example.
054ea2f04b5592b91f8223796cc53979?d=identicon&s=25 Brendon Whateley (brendon)
on 2009-05-06 17:26
(Received via mailing list)
If you are using MySQL, you need to make sure that you are using a DB
engine that supports transactions if you plan to use them.  You will
probably have to use SQL to do the table locking, so if you are using
mysql look in the MySQL manual for the table lock command.  Then write
a (pair) class method to lock and unlock the table.

It might be more helpful to explain why you think you need to lock the
table.
Brendon.

On May 6, 3:54 am, Venkat Eee <rails-mailing-l...@andreas-s.net>
D81c1821bb5fd0bb179a331175751908?d=identicon&s=25 Venkat Eee (venkat_ruby)
on 2009-05-07 07:33
Brendon Whateley wrote:
> If you are using MySQL, you need to make sure that you are using a DB
> engine that supports transactions if you plan to use them.  You will
> probably have to use SQL to do the table locking, so if you are using
> mysql look in the MySQL manual for the table lock command.  Then write
> a (pair) class method to lock and unlock the table.
>
> It might be more helpful to explain why you think you need to lock the
> table.
> Brendon.
>
> On May 6, 3:54�am, Venkat Eee <rails-mailing-l...@andreas-s.net>


Hey, thanks to all...
My code is working..thanks....

ActiveRecord::Base.connection.execute('LOCK TABLES tablename WRITE')
ActiveRecord::Base.connection.execute('UNLOCK TABLES')
1e7782e67bb34c9c67ed19d5cde5f4eb?d=identicon&s=25 Tom Z Meinlschmidt (Guest)
on 2009-05-07 14:53
(Received via mailing list)
Venkat Eee wrote:
>>
>> On May 6, 3:54�am, Venkat Eee <rails-mailing-l...@andreas-s.net>
>
>
> Hey, thanks to all...
> My code is working..thanks....
>
> ActiveRecord::Base.connection.execute('LOCK TABLES tablename WRITE')
> ActiveRecord::Base.connection.execute('UNLOCK TABLES')


imagine you lock the tables.. and then you will be disconnected from the
session for some reason... tables are still locked, no way to work with
them. That's the point dbs have transations...

...

tom

--
===============================================================================
Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

www.meinlschmidt.com  www.maxwellrender.cz  www.lightgems.cz
===============================================================================
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-07 19:19
(Received via mailing list)
On May 7, 1:50 pm, Tom Z Meinlschmidt <to...@meinlschmidt.org> wrote:
>
> > ActiveRecord::Base.connection.execute('LOCK TABLES tablename WRITE')
> > ActiveRecord::Base.connection.execute('UNLOCK TABLES')
>
> imagine you lock the tables.. and then you will be disconnected from the
> session for some reason... tables are still locked, no way to work with
> them. That's the point dbs have transations...

actually mysql releases table locks held by a client if connection to
that client dies (I suppose it might take a while for it to notice
though). Table locks are generally not a very good thing generally.

Fred
This topic is locked and can not be replied to.