You need to make sure that autocommit
is set to false on your connection. This is the key. Then you would do the following steps:
Do your select with the for update clause on the end (select column1, column2 from mytable for update). This will lock the row.
Perform your Update query.
Issue an explicit commit which would release the lock on the row.
Of course remember that locking the row just locks it from modification. Another session could still query those rows. For example if this was an ID and the way to fetch a new ID was to query the table doing a select max(id) + 1 from table
. Locking the row would not prevent another session from doing this select.
Better yet would be to skip the select and update the records in place and use the returning
clause to return to you the new updated values. I have never done it in Ruby OCI8, so I'm not sure if it supports that feature. The docs for that clause in an update are here:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10007.htm#i2126358