Transaction error handling helps you manage problems when multiple database actions happen together. It keeps your data safe and correct.
When you want to update several items at once and need all updates to succeed or none at all.
When you want to avoid partial changes that can cause wrong or broken data.
When you want to catch and fix errors like conflicts or missing items during a transaction.
When you want to retry a transaction if it fails due to temporary issues.
When you want to log or alert about problems during database updates.
Syntax
DynamoDB
Use TransactWriteItems API with a try-catch block in your code.
Example in JavaScript:
try {
await dynamodb.transactWrite(params).promise();
} catch (error) {
// handle error here
}
Transactions in DynamoDB let you group multiple write operations that succeed or fail together.
Errors can happen due to condition check failures, conflicts, or capacity limits.
Examples
This example tries to add a user and update an account balance together. If either fails, no change happens.
This shows how to check the error type to handle specific transaction errors differently.
DynamoDB
try {
await dynamodb.transactWrite(params).promise();
} catch (error) {
if (error.code === 'TransactionCanceledException') {
console.log('Transaction canceled due to condition failure or conflict');
} else {
console.log('Other error:', error.message);
}
}
Sample Program
This program tries to add a product and reduce inventory stock by 5 in one transaction. If stock is less than 5, the transaction fails and prints a message.
5. You want to retry a DynamoDB transaction only if it fails due to a TransactionCanceledException caused by a conditional check failure. Which approach correctly implements this retry logic?
hard
A. Use try-catch but do not check error code; retry only once
B. Use a loop with try-catch; inside catch, check if error.code is 'TransactionCanceledException' and error.message contains 'ConditionalCheckFailed', then retry up to max attempts
C. Retry unconditionally on any error without checking error type
D. Ignore errors and assume transaction will succeed eventually
Solution
Step 1: Understand selective retry conditions
Retry should happen only if error.code is 'TransactionCanceledException' and message indicates conditional check failure.
Step 2: Implement retry with loop and max attempts
Use a loop with try-catch, check error details, and retry up to a limit to avoid infinite loops.
Final Answer:
Use a loop with try-catch; inside catch, check if error.code is 'TransactionCanceledException' and error.message contains 'ConditionalCheckFailed', then retry up to max attempts -> Option B
Quick Check:
Retry only on specific error with loop and checks = Use a loop with try-catch; inside catch, check if error.code is 'TransactionCanceledException' and error.message contains 'ConditionalCheckFailed', then retry up to max attempts [OK]
Hint: Retry only on specific error codes and messages [OK]
Common Mistakes:
Retrying on all errors causing infinite loops
Not checking error.message for conditional failure
Ignoring max retry limits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.