-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Pessimistic/Optimistic Locking of records #64
Comments
Implemented optimistic locking in AR. |
Very cool Qiang, that you just did it :) May I ask what the API looks like for it, e.g. how do we control whether the locking should be applied/used? |
AR has a method named "optimisticLock()". If you override this by returning the column name for storing lock version, then update() and delete() will support optimistic locking. In case a conflict occurs, a StaleObjectException will be thrown. You have to manually generate the hidden field in the form because we don't want to couple the form widget with AR. |
awesome! |
Nice. There's only one thing I'd like to adress: It would be preferable if one could use any unique field for the locking attribute - for example a timestamp named "modified" or similar (being part of a "created" and "modified" timestamp pair) would IMO be an ideal candidate for this, as that is something one might already have in most tables. By not having to create a new/specific field for the locking if there already is a unique field in the table, this would integrate more discretely/seamlessly (one can still create that separate field if wanted). So basically I am asking, is it possible to use any unique column/attribute? If not, can you make it so? Also, is there any use case where this needs to support a composite field/key for the locking? I can't really think of one right now. |
hmm after some consideration 2 things come up.
|
@rawtaz Yes, using existing column for lock version is supported, although timestamp is not precise enough to prevent the conflict (e.g. two people modify the same record within a second.) I don't think we need to consider about composite key. @Tak0r We don't want to automatically generate such hidden fields because it would make the form widget coupled with this very specific AR feature. Even if the hidden field is generated, you still need to handle the logic of assigning the input data to this lock version attribute, which is ugly to do automatically. |
Holy cow. Well, there's yet some fuel to my fire regarding MySQL.. I'm used to PostgreSQL which does it just fine, so I'll happily use my "modified" columns as the locking field :) Anyway, glad to hear one can use any unique column so it doesn't have to be an integer one :D |
Like:
http://api.rubyonrails.org/classes/ActiveRecord/Locking/Pessimistic.html
http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
Yii implementation of Optimistic Locking was posted on the forums.
http://www.yiiframework.com/forum/index.php/topic/5068-activerecord-and-optimistic-locking/page__p__71149#entry71149
This is pretty handy and would be awesome if implemented in the core!
The text was updated successfully, but these errors were encountered: