TypeScript database transactions with error handling
TypeScript Error Handling : Exercise-12 with Solution
Write a TypeScript application that interacts with a database and performs transactions. Implement error propagation to handle database-related errors throughout the application, ensuring transactions are rolled back.
Sample Solution:
TypeScript Code:
// Simulated database module
class Database {
private static data: Map = new Map();
static beginTransaction() {
// Simulate beginning a database transaction
console.log('Transaction started.');
}
static commitTransaction() {
// Simulate committing a database transaction
console.log('Transaction committed.');
}
static rollbackTransaction() {
// Simulate rolling back a database transaction
console.log('Transaction rolled back.');
}
static insert(key: string, value: string) {
// Simulate inserting data into the database
Database.data.set(key, value);
console.log(`Inserted data: ${key} => ${value}`);
}
static retrieve(key: string) {
// Simulate retrieving data from the database
return Database.data.get(key);
}
static throwError() {
// Simulate a database error
throw new Error('Database error');
}
}
// Function to perform a database transaction
async function performTransaction(key: string, value: string) {
Database.beginTransaction();
try {
// Insert data into the database
Database.insert(key, value);
// Simulate a database error
Database.throwError();
Database.commitTransaction();
console.log('Transaction completed successfully.');
} catch (error) {
console.error(`Transaction Error: ${error.message}`);
Database.rollbackTransaction();
}
}
// Main application
function main() {
const key = 'user1';
const value = 'Elmira Judit';
// Perform a database transaction
performTransaction(key, value);
}
// Execute the main application
main();
Explanations:
In the exercise above -
- We have a simulated "Database" class that mimics database operations, including transactions, inserting data, retrieving data, and throwing database errors.
- The "performTransaction()" function begins a database transaction, inserts data into the database, simulates a database error, and either commits or rolls back the transaction based on whether an error occurred.
- The "main()" function initiates the main application logic, where we perform a database transaction.
- Finally, when you run this application, you'll see that it logs the transaction's progress and handles database-related errors with transaction rollback.
Output:
Transaction started. Inserted data: user1 => Elmira Judit Transaction Error: Database error Transaction rolled back.
TypeScript Editor:
See the Pen TypeScript by w3resource (@w3resource) on CodePen.
Previous: TypeScript Multi-Step workflow with error handling.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/typescript-exercises/typescript-error-handling-exercise-12.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics