Looking through the api on record locking I'm confused.
given the stateless nature of html, it's entirely possible that someone
might click on 'edit' and then simply abandon their 'edit' functions
without ever saving - which tells me that I should just have a column in
each table called 'lock_version' with a default of 0 - (Opportunistic
locking) right?
Then I need a method in each model employing record locking to rescue
any exceptions raised by multiple people trying to edit without saving
and in essence checking out the data again and discarding their edits.
Do I have this right?
Craig
Yes, you are right. It is dangerous to leave a record locked any longer than necessary, and very dangerous to leave it locked if you don't absolutely know the transaction will complete. If you want optimistic locking, add a column called lock_version. If you want pessimistic locking, see this:
http://ryandaigle.com/articles/2006/6/27/whats-new-in-edge-rails-pessimistic-locking
It's safe to use optimistic locking across http requests if you don't mind failed updates when collisions occur. Otherwise, consider pessimistic, as described.
G'luck!
OK - something must be wrong with my controller code then.
I added 'lock_version' to my 'clients' table and restarted my
mongrels.
in script/console, opportunistic locking works as advertised in the
API
but using 2 separate browsers (1 Firefox, 1 Konqueror) with 2 separate
sessions, I can update either in any order without an issue and the
updates are saved and the 'lock_version' value simply increments.
This is intended. Opportunistic locking guards you from changes
happening behind your back in between you doing foo = Foo.find(123)
and foo.save
Pessimistic locking has a similar sort of the database transaction
it's enclosed in, and you wouldn't want that lasting beyond a single
request (apart from anything else, if you have more than 1 mongrel,
there's no guarantee you won't get some requests served by 1 mongrel
and one by the other).
It sounds as though you're worried about the record changing in
between the user viewing the form and you saving the record. There's
no support for that builtin, but I think you could extend the
principle of optimistic locking (include the current lock_version as a
hidden field on the form)
Fred
Jodi Showers wrote:
@client.attributes = params[:client]
@client.valid?
isn't
@client.update_attributes params[:client]
a shortcut for the example above ?
Lionel
No, since update_attributes actually performs the save, whereas valid? will just run the validations.
Fred
Frederick Cheung wrote:
[...]
No, since update_attributes actually performs the save, whereas valid? will just run the validations.
Whoops. I had a feeling my question was stupid. Still need coffee...