使用 java 将项目存储到 DynamoDB 时发生 ConditionalCheckFailed 异常
ConditionalCheckFailed Exception occurs while storing an item into DynamoDB using java
我正在使用 DynamoDBMapper 将项目保存到 DynamoDB 中,但没有任何 condition/expression 设置。但是每次保存对象时我都会收到 ConditionalCheckFailed 异常。参考下面的代码。
注意:我是运行这段代码在5个线程下。
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;
}
}
DynamoDB 启用乐观锁定策略以确保您正在更新(或删除)的客户端项与DynamoDB 中的项相同。注释 @DynamoDBVersionAttribute
用于此目的。
第一次 - 保存应该成功
第二次-调用保存时需要提供版本。例如:-
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.
关闭乐观锁:-
将 SaveBehavior 设置为 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.
我正在使用 DynamoDBMapper 将项目保存到 DynamoDB 中,但没有任何 condition/expression 设置。但是每次保存对象时我都会收到 ConditionalCheckFailed 异常。参考下面的代码。
注意:我是运行这段代码在5个线程下。
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;
}
}
DynamoDB 启用乐观锁定策略以确保您正在更新(或删除)的客户端项与DynamoDB 中的项相同。注释 @DynamoDBVersionAttribute
用于此目的。
第一次 - 保存应该成功
第二次-调用保存时需要提供版本。例如:-
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.
关闭乐观锁:-
将 SaveBehavior 设置为 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.