Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the @Transactional annotation in Spring Boot?
The @Transactional annotation manages database transactions automatically. It ensures that a group of operations either all succeed or all fail, keeping data consistent.
Click to reveal answer
beginner
What happens if an exception occurs inside a method annotated with @Transactional?
If a runtime exception occurs, the transaction is rolled back, undoing all changes made during that transaction to keep data safe.
Click to reveal answer
intermediate
Can @Transactional be applied at both class and method levels? What is the effect?
Yes. When applied at the class level, all methods inherit the transaction settings. Method-level @Transactional overrides class-level settings for that method.
Click to reveal answer
intermediate
What is the default propagation behavior of @Transactional?
The default propagation is REQUIRED, meaning the method will join an existing transaction if available or start a new one if not.
Click to reveal answer
beginner
How does @Transactional improve data consistency in multi-step database operations?
It groups multiple database operations into a single transaction, so either all operations succeed or all fail together, preventing partial updates.
Click to reveal answer
What does @Transactional do when a runtime exception is thrown inside the method?
AStarts a new transaction
BCommits the transaction
CRolls back the transaction
DIgnores the exception
✗ Incorrect
@Transactional rolls back the transaction on runtime exceptions to keep data consistent.
Where can you apply the @Transactional annotation?
AOnly on methods
BOn both classes and methods
COnly on classes
DOnly on interfaces
✗ Incorrect
@Transactional can be applied at both class and method levels. Method-level settings override class-level ones.
What is the default propagation behavior of @Transactional?
AREQUIRED
BMANDATORY
CREQUIRES_NEW
DSUPPORTS
✗ Incorrect
The default propagation is REQUIRED, which joins an existing transaction or creates a new one if none exists.
If a checked exception occurs inside a @Transactional method, what happens by default?
ATransaction rolls back
BTransaction pauses
CTransaction restarts
DTransaction commits
✗ Incorrect
By default, checked exceptions do not cause rollback unless configured explicitly.
Why is transaction management important in database operations?
ATo ensure data consistency
BTo speed up queries
CTo reduce server load
DTo encrypt data
✗ Incorrect
Transaction management ensures that multiple related operations complete together, keeping data consistent.
Explain how @Transactional helps maintain data consistency in Spring Boot applications.
Think about what happens when something goes wrong during multiple database steps.
You got /4 concepts.
Describe the difference between applying @Transactional at the class level versus the method level.
Consider how you might want different transaction rules for different methods.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @Transactional in a Spring Boot application?
easy
A. To ensure multiple database operations are executed as a single unit and rollback on failure
B. To speed up database queries by caching results
C. To automatically generate database schema from entities
D. To log all database queries for debugging
Solution
Step 1: Understand the role of @Transactional
@Transactional groups multiple database operations so they succeed or fail together.
Step 2: Identify the effect on data consistency
If any operation fails, all changes are rolled back to keep data correct.
Final Answer:
To ensure multiple database operations are executed as a single unit and rollback on failure -> Option A
Quick Check:
@Transactional = atomic database actions [OK]
Hint: Think: all-or-nothing for database changes [OK]
Common Mistakes:
Confusing @Transactional with caching or logging
Thinking it speeds up queries
Assuming it auto-generates schema
2. Which of the following is the correct way to apply @Transactional to a method in a Spring Boot service class?
easy
A. @Transactional public void updateData() { ... }
B. public @Transactional void updateData() { ... }
C. public void updateData() @Transactional { ... }
D. public void updateData() { @Transactional ... }
Solution
Step 1: Recall correct annotation placement
Annotations like @Transactional go before the method signature.
Step 2: Check each option's syntax
@Transactional public void updateData() { ... } places @Transactional correctly before the method declaration.
Final Answer:
@Transactional public void updateData() { ... } -> Option A
Quick Check:
Annotation before method = correct syntax [OK]
Hint: Annotations always go before method signature [OK]
Common Mistakes:
Placing annotation inside method body
Putting annotation after method signature
Using annotation as a modifier keyword
3. Consider this Spring Boot service method annotated with @Transactional:
@Transactional
public void saveUserAndAccount(User user, Account account) {
userRepository.save(user);
accountRepository.save(account);
if(account.getBalance() < 0) {
throw new RuntimeException("Negative balance not allowed");
}
}
What happens if account.getBalance() < 0 is true during execution?
medium
A. An error is logged but changes are committed
B. Only the user is saved, account save is rolled back
C. Both user and account are saved to the database
D. Neither user nor account is saved; transaction rolls back
Solution
Step 1: Understand rollback behavior of @Transactional
By default, RuntimeExceptions cause the transaction to rollback all changes.
Step 2: Analyze the exception thrown
The method throws RuntimeException if balance is negative, triggering rollback.
Final Answer:
Neither user nor account is saved; transaction rolls back -> Option D
Quick Check:
RuntimeException triggers rollback = no data saved [OK]
Hint: Exception inside @Transactional rolls back all changes [OK]
If updateB() throws a checked exception (not RuntimeException), what will happen to the transaction?
medium
A. Transaction will rollback automatically
B. Transaction will commit despite the exception
C. Transaction will rollback only if exception is caught and rethrown as RuntimeException
D. Transaction will pause until exception is handled
Solution
Step 1: Recall default rollback rules of @Transactional
By default, only unchecked exceptions (RuntimeException) cause rollback.
Step 2: Analyze checked exception behavior
Checked exceptions do not trigger rollback unless configured or rethrown as RuntimeException.
Final Answer:
Transaction will commit despite the exception -> Option B
Quick Check:
Checked exceptions do not rollback by default [OK]
Hint: Only RuntimeExceptions rollback by default [OK]
Common Mistakes:
Assuming all exceptions cause rollback
Not knowing difference between checked and unchecked exceptions
Expecting rollback without configuration
5. You have a Spring Boot service with two methods:
public void outerMethod() {
innerMethod();
}
@Transactional
public void innerMethod() {
// database updates
if(someCondition) throw new RuntimeException();
}
If outerMethod() is called, will the transaction rollback if innerMethod() throws the exception? Assume default proxy-based Spring transaction management.
hard
A. Yes, but only if outerMethod is also annotated with @Transactional
B. Yes, transaction rolls back because innerMethod is @Transactional
C. No, transaction does not rollback because outerMethod is not @Transactional and calls innerMethod internally
D. No, because RuntimeException does not trigger rollback in this case
Solution
Step 1: Understand Spring proxy behavior for @Transactional
Spring uses proxies, so self-invocation (method calling another in same class) bypasses proxy and ignores @Transactional.
Step 2: Analyze effect on transaction rollback
Since outerMethod calls innerMethod directly, @Transactional on innerMethod is ignored, so no transaction starts and no rollback occurs.
Final Answer:
No, transaction does not rollback because outerMethod is not @Transactional and calls innerMethod internally -> Option C
Quick Check:
Self-call bypasses @Transactional proxy = no rollback [OK]
Hint: Self-calls ignore @Transactional proxy, no transaction started [OK]
Common Mistakes:
Assuming @Transactional always works on internal calls
Thinking RuntimeException always triggers rollback here