Inaccessible
Inaccessible

Reputation: 1560

ConditionalCheckFailed Exception occurs while storing an item into DynamoDB using java

I am using DynamoDBMapper to save an item into DynamoDB without any condition/expression set. But I am getting ConditionalCheckFailed exception each time I save the object. Refer code below.

Note: I am running this code under 5 threads.

Main.java

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.regions.Region;

AmazonDynamoDBClient db = new AmazonDynamoDBClient();
Region region = Region.getRegion(regions);
db.setRegion(region);
DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.DEFAULT,
            new DynamoDBMapperConfig(TableNameOverride.withTableNameReplacement("myTableName")));
DynamoDBMapper mapper = new DynamoDBMapper(db, config);

UserDocument userDocument = new UserDocument("UD001", "TestUser");
mapper.save(userDocument);  # Getting ConditionalCheckFailed exception

UserDocument.java

public class UserDocument {
    public UserDocument(String id, String account) {
        this.id = id;
        this.account = account;
    }

    @DynamoDBHashKey
    @DynamoDBAttribute(attributeName = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @DynamoDBVersionAttribute
    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    @DynamoDBAttribute(attributeName = "account")
    public int getAccount() {
        return account;
    }

    public void setAccount(int account) {
        this.account = account;
    }
}

Upvotes: 1

Views: 4350

Answers (1)

notionquest
notionquest

Reputation: 39186

DynamoDB enables optimistic locking strategy to ensure that the client-side item that you are updating (or deleting) is the same as the item in DynamoDB. The annotation @DynamoDBVersionAttribute is used for that.

First time - Save should work successfully

Second time - You need to provide the version while calling the save. For example:-

userDocument.setVersion(1L);

@DynamoDBVersionAttribute

To support optimistic locking, the AWS SDK for Java provides the @DynamoDBVersionAttribute annotation. In the mapping class for your table, you designate one property to store the version number, and mark it using this annotation. When you save an object, the corresponding item in the DynamoDB table will have an attribute that stores the version number.

To switch off the optimistic locking:-

Set the SaveBehavior as CLOBBER

DynamoDBMapperConfig config = new DynamoDBMapperConfig(new DynamoDBMapperConfig(
                DynamoDBMapperConfig.SaveBehavior.CLOBBER),             
                new DynamoDBMapperConfig(TableNameOverride.withTableNameReplacement("userdocument")));

To disable optimistic locking, you can change the DynamoDBMapperConfig.SaveBehavior enumeration value from UPDATE to CLOBBER. You can do this by creating a DynamoDBMapperConfig instance that skips version checking and use this instance for all your requests.

Optimistic locking

Upvotes: 3

Related Questions