use of software.amazon.awssdk.services.dynamodb.model.CancellationReason in project aws-sdk-java-v2 by aws.
the class TransactWriteItemsTest method mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail.
@Test
public void mixedCommands_returnValuesOnConditionCheckFailureSet_allConditionsFail() {
mappedTable1.putItem(r -> r.item(RECORDS_1.get(0)));
mappedTable1.putItem(r -> r.item(RECORDS_1.get(1)));
mappedTable2.putItem(r -> r.item(RECORDS_2.get(0)));
Expression conditionExpression = Expression.builder().expression("#attribute = :attribute").expressionValues(singletonMap(":attribute", stringValue("99"))).expressionNames(singletonMap("#attribute", "attribute")).build();
Key key0 = Key.builder().partitionValue(0).build();
Key key1 = Key.builder().partitionValue(1).build();
ReturnValuesOnConditionCheckFailure returnValues = ReturnValuesOnConditionCheckFailure.ALL_OLD;
TransactPutItemEnhancedRequest<Record2> putItemRequest = TransactPutItemEnhancedRequest.builder(Record2.class).conditionExpression(conditionExpression).item(RECORDS_2.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
TransactUpdateItemEnhancedRequest<Record1> updateItemRequest = TransactUpdateItemEnhancedRequest.builder(Record1.class).conditionExpression(conditionExpression).item(RECORDS_1.get(0)).returnValuesOnConditionCheckFailure(returnValues).build();
TransactDeleteItemEnhancedRequest deleteItemRequest = TransactDeleteItemEnhancedRequest.builder().key(key1).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build();
TransactWriteItemsEnhancedRequest transactWriteItemsEnhancedRequest = TransactWriteItemsEnhancedRequest.builder().addPutItem(mappedTable2, putItemRequest).addUpdateItem(mappedTable1, updateItemRequest).addConditionCheck(mappedTable1, ConditionCheck.builder().key(key0).conditionExpression(conditionExpression).returnValuesOnConditionCheckFailure(returnValues).build()).addDeleteItem(mappedTable1, deleteItemRequest).build();
try {
enhancedClient.transactWriteItems(transactWriteItemsEnhancedRequest);
fail("Expected TransactionCanceledException to be thrown");
} catch (TransactionCanceledException e) {
List<CancellationReason> cancellationReasons = e.cancellationReasons();
assertThat(cancellationReasons.size(), is(4));
cancellationReasons.forEach(r -> assertThat(r.item().isEmpty(), is(false)));
}
}
use of software.amazon.awssdk.services.dynamodb.model.CancellationReason in project scalardb by scalar-labs.
the class BatchHandler method handle.
/**
* Execute the specified list of {@link Mutation}s in batch. All the {@link Mutation}s in the list
* must be for the same partition.
*
* @param mutations a list of {@code Mutation}s to execute
* @throws NoMutationException if at least one of conditional {@code Mutation}s failed because it
* didn't meet the condition
*/
public void handle(List<? extends Mutation> mutations) throws ExecutionException {
if (mutations.size() > 25) {
throw new IllegalArgumentException("DynamoDB cannot batch more than 25 mutations at once.");
}
TableMetadata tableMetadata = metadataManager.getTableMetadata(mutations.get(0));
TransactWriteItemsRequest.Builder builder = TransactWriteItemsRequest.builder();
List<TransactWriteItem> transactItems = new ArrayList<>();
mutations.forEach(m -> transactItems.add(makeWriteItem(m, tableMetadata)));
builder.transactItems(transactItems);
try {
client.transactWriteItems(builder.build());
} catch (TransactionCanceledException e) {
boolean allReasonsAreTransactionConflicts = true;
for (CancellationReason reason : e.cancellationReasons()) {
if (reason.code().equals("ConditionalCheckFailed")) {
throw new NoMutationException("no mutation was applied.", e);
}
if (!reason.code().equals("TransactionConflict") && !reason.code().equals("None")) {
allReasonsAreTransactionConflicts = false;
}
}
if (allReasonsAreTransactionConflicts) {
// RetriableExecutionException
throw new RetriableExecutionException(e.getMessage(), e);
}
throw new ExecutionException(e.getMessage(), e);
} catch (DynamoDbException e) {
throw new ExecutionException(e.getMessage(), e);
}
}
Aggregations