Question on Data Concurrency


#1

In Rails 2.2.2, if I retrieve a record using a Model.find method (any
of the finds), make changes to it, and then save it, is there a
possibility that another user (on a different thread, maybe a faster
machine) could read the same record after me, but update it before
me? If so, then the other user’s changes would be lost (a data
concurrency problem).

How does Rails prevent this?

Is there a way to do something like
Model.save_only_if_updated_at_is_unchanged ?
Is there a find_and_lock, find_for_update, or other similar function?

This is different than dirty records, because it is comparing the
record I have in my session to the same record in the database to
ensure that no one else changed it.

Thank you,

Steve


#2

On 17 Jan 2009, at 16:45, srj wrote:

In Rails 2.2.2, if I retrieve a record using a Model.find method (any
of the finds), make changes to it, and then save it, is there a
possibility that another user (on a different thread, maybe a faster
machine) could read the same record after me, but update it before
me? If so, then the other user’s changes would be lost (a data
concurrency problem).

Yes

How does Rails prevent this?

Is there a way to do something like
Model.save_only_if_updated_at_is_unchanged ?
Is there a find_and_lock, find_for_update, or other similar function?

Look up optimistic locking.

Fred