Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb ConditionalCheckFailedException on read / update operation - java sdk

I'm new to DynamoDB and trying out a sample scenario using Transaction support. I'm using the same entity provided in dynamodb-transaction library. Only difference is I've added a range key with the hash key. Here's the table definition:

  • ItemId --> hash key , string
  • ItemName --> range key, string

@DynamoDBTable(tableName = "Item")
public static class ExampleItem {
    private String itemId;
    private String value;
    private String itemName;
    private Long version;
    @DynamoDBHashKey(attributeName = "ItemId")
    public String getItemId() {
        return itemId;
    }
    public void setItemId(String itemId) {
        this.itemId = itemId;
    }
    @DynamoDBRangeKey(attributeName="ItemName")
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
    @DynamoDBVersionAttribute
    public Long getVersion() {
        return version;
    }
    public void setVersion(Long version) {
        this.version = version;
    }
}

As you can see, I'm using the version attribute as well. Now, I'm trying to execute a simple create or update scenario using transaction/dbmapper. Here's the code snippet.

Transaction t1 = txManager.newTransaction();
ExampleItem keyItem = new ExampleItem();
keyItem.setItemId("Item1");
keyItem.setItemName("USA");
ExampleItem item = t1.load(keyItem);
if (item != null) {
    item.setValue("Magenta");
    item.setItemName("UK");
    t1.save(item);
} else {
    item = new ExampleItem();
    item.setItemId(keyItem.getItemId());
    item.setItemName("USA");
    item.setValue("Violet");
    t1.save(item);
}
t1.commit();
t1.delete();

I'm able to add the record without any issue, but I'm having a problem when I'm trying to read the record and update any attribute. I'm getting the following exception:

Uncaught exception:com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: Item {ItemId={S: Item1,}, ItemName={S: UK,}} had unexpected attributes: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: expected attribute(s) {version={Value: {N: 1,},Exists: true}} but found null
com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: Item {ItemId={S: Item1,}, ItemName={S: UK,}} had unexpected attributes: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: expected attribute(s) {version={Value: {N: 1,},Exists: true}} but found null

Looks like it has something to do with version, but not sure where am I going wrong. Any pointers will be appreciated.

-Thanks

Shamik

like image 398
Shamik Avatar asked Oct 23 '14 16:10

Shamik


1 Answers

Usualy this occurs when your keys are same for more then one element in dynamodb. For example if you have Folder with id as hashKey and folderName as rangeKey, You wont save the Folder again where your hash(hashkey+rangeKey) would be same.

like image 177
M-sAnNan Avatar answered Oct 16 '22 21:10

M-sAnNan