DynamoDBMapper 乐观锁定 – 是否必须先调用 load 方法?

huangapple go评论156阅读模式
英文:

DynamoDBMapper Optimistic Locking - Do I have to call the load first?

问题

如果在更新对象时没有首先调用加载操作,如果发生版本不匹配,乐观锁定仍然会起作用吗?换句话说,是否必须调用加载操作才能使乐观锁定起作用?

英文:

I am trying to use Optimistic Locking for an application that I am creating. On the AWS website it mentions that "With optimistic locking, each item has an attribute that acts as a version number. If you retrieve an item from a table, the application records the version number of that item. You can update the item, but only if the version number on the server side has not changed. If there is a version mismatch, it means that someone else has modified the item before you did. The update attempt fails, because you have a stale version of the item." If I update an object without calling load first, does Optimistic Locking still work if a version mismatch were to happen? In other words, is call load mandatory for Optimistic Locking to work?

答案1

得分: 2

换句话说,调用加载对于乐观锁定是强制性的吗?

不是的,但通常情况下,如果在尝试更新之前不加载项目,您将难以使用乐观锁定。

原因是因为它的工作原理:当您发送用于乐观锁定的条件性更新请求时,条件通常是在项目上的**version**属性的,该值也会在同一更新请求中更新(通常递增1)。

因此,为了使更新起作用,需要告诉DynamoDB version 属性的值必须是什么。

如果在尝试发送具有乐观锁定的条件性更新请求之前不加载项目,您怎么知道当前版本是多少呢?如果您不知道该版本是什么,那么您通常不能期望条件性更新请求起作用。


话虽如此,在某些情况下,可能不使用乐观锁定对于某些请求是有道理的。

例如,假设您需要更改给定属性的值,而项目的状态不会改变您的要求。在这种情况下,您不需要乐观锁定此更新:您可以简单地告诉DynamoDB执行更新(同时递增您的version属性!)。任何其他并发操作将在它们是否也不使用乐观锁定(即不使用条件性更新)的情况下成功,或者如果它们使用了乐观锁定则会失败。

英文:

> In other words, is call load mandatory for Optimistic Locking to work?

It is not, but in general you'll have difficulty using optimistic locking if you don't load the item before trying to update it.

The reason is because of how it works: when you send a conditional update request for optimistic locking, the condition typically being on the value of a version attribute on the item, that is also updated (generally incremented by 1) in the same update request.

Thus, in order for an update to work, you need to tell DynamoDB what the value of that version attribute must be.

If you don't load the item before trying to send that conditional update request for optimistic locking, how would you know what the current version is? If you don't know what that version is, then you can't in general expect the conditional update request to work.


That said, there are some circumstances where it might make sense to not use optimistic locking for some requests.

For example, let's say that you need to change the value of a given attribute, and nothing in the state of the item would change your requirements. In this case, you don't need optimistic locking for this update: you can simply tell DynamoDB to perform the update (while also simultaneously incrementing your version attribute!). Any other concurrent operations will either succeed if they are also not using optimistic locking (i.e., not using a conditional update), or they will fail if they are using it.

huangapple
  • 本文由 发表于 2020年7月23日 01:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63040110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定