A transaction is a group of operations performed as a single unit. All operations must complete successfully, or none are applied, to keep data correct.
Click to reveal answer
intermediate
What does ACID stand for in transaction handling?
ACID means Atomicity, Consistency, Isolation, and Durability. These are rules to make sure transactions work safely and correctly.
Click to reveal answer
intermediate
Why is isolation important in transactions?
Isolation keeps transactions separate so they don't interfere with each other. This avoids problems like reading wrong or partial data.
Click to reveal answer
beginner
What happens if a transaction fails during execution?
If a transaction fails, all changes made during it are undone (rolled back) so the database stays correct and unchanged.
Click to reveal answer
advanced
How do you start and end a transaction in GraphQL with a database?
You start a transaction by telling the database to begin, then run your operations, and finally commit to save or rollback to undo if errors happen.
Click to reveal answer
What does the 'Atomicity' property ensure in a transaction?
AAll parts of the transaction succeed or none do
BData is always consistent after transaction
CTransactions run at the same time without interference
DChanges are saved permanently
✗ Incorrect
Atomicity means the transaction is all or nothing: either all steps succeed or none are applied.
Which of the following is NOT part of ACID properties?
AAtomicity
BConsistency
CIsolation
DIndexing
✗ Incorrect
Indexing is a database optimization, not an ACID property.
What does 'rollback' do in transaction handling?
AUndoes all changes made in the transaction
BStarts a new transaction
CSaves all changes permanently
DLocks the database
✗ Incorrect
Rollback reverses all changes made during the transaction to keep data safe.
Why is isolation important when multiple transactions run at the same time?
ATo speed up the database
BTo allow partial data reads
CTo prevent transactions from interfering with each other
DTo save memory
✗ Incorrect
Isolation keeps transactions separate so they don't affect each other's data.
In GraphQL, how do you ensure multiple database operations are treated as one transaction?
ABy using a single query
BBy starting a transaction, running operations, then committing or rolling back
CBy running operations in parallel
DBy using multiple mutations without control
✗ Incorrect
You explicitly start a transaction, perform operations, then commit or rollback to keep data consistent.
Explain the ACID properties and why they matter in transaction handling.
Think about how each property helps keep data correct and reliable.
You got /5 concepts.
Describe the steps to handle a transaction in a GraphQL API connected to a database.
Consider how you control the database actions inside your GraphQL resolver.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using transactions in a database when working with GraphQL?
easy
A. To group multiple actions so they all succeed or fail together
B. To speed up query execution by caching results
C. To automatically generate GraphQL schemas
D. To encrypt data sent over the network
Solution
Step 1: Understand what transactions do
Transactions ensure that a set of database operations are treated as a single unit.
Step 2: Identify the purpose in GraphQL context
This means either all changes succeed or none do, keeping data consistent.
Final Answer:
To group multiple actions so they all succeed or fail together -> Option A
Quick Check:
Transactions = atomic group actions [OK]
Hint: Transactions keep multiple changes safe together [OK]
Common Mistakes:
Thinking transactions speed up queries
Confusing transactions with schema generation
Assuming transactions encrypt data
2. Which of the following is the correct way to start a transaction in a GraphQL resolver using a typical backend?
easy
A. const transaction = await db.rollback();
B. const transaction = db.commit();
C. const transaction = await db.startTransaction();
D. const transaction = db.query('BEGIN');
Solution
Step 1: Identify how to start a transaction
Starting a transaction usually involves calling a method like startTransaction() asynchronously.
Step 2: Check options for correctness
const transaction = await db.startTransaction(); correctly uses await and startTransaction(), while others are commit, rollback, or raw query.
Final Answer:
const transaction = await db.startTransaction(); -> Option C
Quick Check:
Start transaction = startTransaction() [OK]
Hint: Start transactions with async startTransaction() method [OK]
Common Mistakes:
Using commit or rollback to start a transaction
Using raw query strings instead of methods
Missing await for async calls
3. Given this GraphQL resolver snippet, what will be the output if an error occurs during updateUser()?
5. You want to update a user's profile and their related settings in one transaction using GraphQL. Which approach ensures both updates succeed or fail together?