¿Cuándo debo preocuparme por el bloqueo de ActiveRecord?

I read about optimistic locking and pessimistic locking feature of ActiveRecord and I am not using them at all in my Rails code and things seem fine. What is a good rule of thumb to know when to use them? When two people are trying to edit the same object would be one example I guess.

preguntado el 01 de febrero de 12 a las 22:02

1 Respuestas

You need to use them, or some other solution to the same problem, when you can have concurrent changes to the same data, and where there is no clear rule for which change "wins". That is a subset of the times that two people edit the same object.

In general, optimistic locking is good when conflicts are unlikely, because they have less overhead for people: you can just edit, and very occasionally get told that you need to redo the work, because someone else changed the object.

Pessimistic locking is good when conflicts are likely, and the amount of effort to redo the work is high, and your users don't want to stay making changes for long, and they can be trusted to be polite about, eg, releasing the lock when they are done.

Both work well if, and only if, you have relatively small numbers of users interested in the same data. Both models scale poorly to large numbers of concurrent edits, because they either force the user to redo work when there is a conflict, or to wait until someone else is done before they get their work done.

When you expect conflicts, and more than a very few users operating on the same data, you want to look to more advanced approaches like CQRS to solve the concurrency problems.

Respondido 02 Feb 12, 02:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.