DynamoDB optimistic locking

Optimistic locking  is a way to ensure the same strategy is being updated (or delete) client project with Amazon DynamoDB in the project. If you use this strategy, database write covered by a written others, and vice versa will be prevented.

note

  • DynamoDB global table using the "last writer to prevail" in principle between concurrent updates. If you use the global table, places the last writer strategy prevail. Therefore, in this case, the lockout policy does not work as expected.

  • DynamoDBMapper transaction operations are not supported optimistic locking.

When using optimistic locking, each project has a version number to act as an attribute. If you retrieve items in the table, the application will record the version number of the project. You can update the project, but only when the server-side update the version number has not changed. If there is a version mismatch, it means that other people modify the project before you. Update attempt fails, it is because you have an outdated version of the project. If this happens, you can simply by searching the project and then try to update and try again. Optimistic locking prevents you from accidentally overwriting other people changes. It also prevents others from accidentally overwriting your changes. To support optimistic locking, AWS SDK for Java provides  @DynamoDBVersionAttribute comments. To apply to the table mapping class, you need to specify the attributes for storing the version number and use this annotation to mark them. When you save an object, DynamoDB tables corresponding items will have attributes stored corresponding version number. DynamoDBMapper It will assign a version number when you first save the object, and the value of the version number is incremented each time a project update. Only when the client object versions with DynamoDB table corresponding revision of the project to match your update, or delete request to be successful.

 1 @DynamoDBTable(tableName="ProductCatalog")
 2 public class CatalogItem {
 3     
 4     private Integer id;
 5     private String title;
 6     private String ISBN;
 7     private Set<String> bookAuthors;
 8     private String someProp;
 9     private Long version;
10 
11     @DynamoDBHashKey(attributeName="Id")  
12     public Integer getId() { return id; }
13     public void setId(Integer Id) { this.id = Id; }
14     
15     @DynamoDBAttribute(attributeName="Title")  
16     public String getTitle() { return title; }    
17     public void setTitle(String title) { this.title = title; }
18     
19     @DynamoDBAttribute(attributeName="ISBN")  
20     public String getISBN() { return ISBN; }    
21     public void setISBN(String ISBN) { this.ISBN = ISBN;}
22     
23     @DynamoDBAttribute(attributeName = "Authors")
24     public Set<String> getBookAuthors() { return bookAuthors; }    
25     public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; }
26     
27     @DynamoDBIgnore
28     public String getSomeProp() { return someProp;}
29     public void setSomeProp(String someProp) {this.someProp = someProp;}
30     
31     @DynamoDBVersionAttribute
32     public Long getVersion() { return version; }
33     public void setVersion(Long version) { this.version = version;}
34 }

 

Guess you like

Origin www.cnblogs.com/cloudrivers/p/11618677.html