Bird
Raised Fist0
Spring Bootframework~10 mins

Read-only transactions in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the transaction as read-only.

Spring Boot
@Transactional(readOnly = [1])
public void fetchData() {
    // method logic
}
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
C"true"
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string like "true" instead of boolean true.
Setting readOnly to false which means the transaction can write data.
2fill in blank
medium

Complete the code to import the correct annotation for transactions.

Spring Boot
import org.springframework.transaction.annotation.[1];

@Transactional(readOnly = true)
public void getData() {
    // method logic
}
Drag options to blanks, or click blank then click option'
AComponent
BService
CTransactional
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated annotations like @Service or @Component.
Confusing repository annotation with transaction annotation.
3fill in blank
hard

Fix the error in the transaction annotation to make it read-only.

Spring Boot
@Transactional(readOnly = [1])
public void loadData() {
    // method logic
}
Drag options to blanks, or click blank then click option'
A"false"
Bfalse
C"true"
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using string values like "true" or "false" instead of boolean.
Setting readOnly to false which allows writes.
4fill in blank
hard

Fill both blanks to create a read-only transaction with propagation set to SUPPORTS.

Spring Boot
@Transactional(readOnly = [1], propagation = Propagation.[2])
public void queryData() {
    // method logic
}
Drag options to blanks, or click blank then click option'
Atrue
BREQUIRED
CSUPPORTS
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using false for readOnly which allows writes.
Using REQUIRED propagation which starts a new transaction.
5fill in blank
hard

Fill all three blanks to define a read-only transaction with isolation level READ_COMMITTED and timeout 30 seconds.

Spring Boot
@Transactional(readOnly = [1], isolation = Isolation.[2], timeout = [3])
public void fetchRecords() {
    // method logic
}
Drag options to blanks, or click blank then click option'
Afalse
BREAD_COMMITTED
C30
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using false for readOnly which allows writes.
Using wrong isolation level like SERIALIZABLE.
Setting timeout as a string instead of integer.

Practice

(1/5)
1. What is the main purpose of using @Transactional(readOnly = true) in Spring Boot?
easy
A. To allow data modifications within the transaction
B. To optimize performance by indicating the method only reads data
C. To disable transaction management entirely
D. To automatically commit changes after method execution

Solution

  1. Step 1: Understand the role of read-only transactions

    Read-only transactions tell Spring the method will only read data, not modify it.
  2. Step 2: Recognize performance benefits

    This allows Spring and the database to optimize the transaction for reading, improving performance.
  3. Final Answer:

    To optimize performance by indicating the method only reads data -> Option B
  4. Quick Check:

    Read-only = optimize read performance [OK]
Hint: Read-only means no data changes allowed, just reading [OK]
Common Mistakes:
  • Thinking readOnly=true allows data changes
  • Confusing readOnly with disabling transactions
  • Assuming it commits changes automatically
2. Which of the following is the correct way to declare a read-only transaction on a method in Spring Boot?
easy
A. @Transactional(readOnly = true)
B. @Transactional(readOnly)
C. @Transactional(enabled = true)
D. @Transactional(readOnly = false)

Solution

  1. Step 1: Recall the correct syntax for read-only transactions

    The correct attribute is readOnly = true inside the @Transactional annotation.
  2. Step 2: Check each option

    @Transactional(readOnly = true) uses the exact correct syntax. Others are either wrong attribute names or values.
  3. Final Answer:

    @Transactional(readOnly = true) -> Option A
  4. Quick Check:

    Correct syntax uses readOnly = true [OK]
Hint: Use readOnly = true exactly inside @Transactional [OK]
Common Mistakes:
  • Using readOnly without = true
  • Using readOnly = false by mistake
  • Using non-existent attributes like enabled
3. Consider this Spring Boot method:
@Transactional(readOnly = true)
public List<User> getUsers() {
    userRepository.save(new User("John"));
    return userRepository.findAll();
}
What will happen when this method runs?
medium
A. An exception will be thrown because save is called in a read-only transaction
B. The save call will be ignored, but findAll will return existing users
C. The new user "John" will be saved and returned in the list
D. The method will run normally without any restrictions

Solution

  1. Step 1: Understand read-only transaction restrictions

    Read-only transactions prevent data modifications like save or update operations.
  2. Step 2: Analyze the method behavior

    Calling save inside a read-only transaction causes Spring or the database to throw an exception.
  3. Final Answer:

    An exception will be thrown because save is called in a read-only transaction -> Option A
  4. Quick Check:

    Save in read-only transaction = exception [OK]
Hint: Save inside read-only transaction causes error [OK]
Common Mistakes:
  • Assuming save silently fails
  • Thinking save works normally in read-only
  • Ignoring transaction settings
4. You have this method:
@Transactional(readOnly = true)
public void updateUserName(Long id, String name) {
    User user = userRepository.findById(id).orElseThrow();
    user.setName(name);
}
Why might this method fail to update the user's name?
medium
A. Because the method is missing @Transactional annotation
B. Because findById does not return a user
C. Because setName is not a valid method
D. Because readOnly = true prevents any data changes inside the transaction

Solution

  1. Step 1: Understand effect of readOnly = true on data changes

    Read-only transactions prevent changes from being saved to the database.
  2. Step 2: Analyze the method's update attempt

    Even though the user object is modified, the transaction will not commit changes due to readOnly=true.
  3. Final Answer:

    Because readOnly = true prevents any data changes inside the transaction -> Option D
  4. Quick Check:

    readOnly = true blocks data updates [OK]
Hint: readOnly = true blocks saving changes [OK]
Common Mistakes:
  • Assuming object changes auto-save without commit
  • Thinking findById always fails
  • Ignoring transaction annotation effects
5. You want to create a service method that fetches user data without risking accidental updates and improves performance. Which approach is best?
hard
A. Do not use any transaction annotation and perform all operations directly
B. Use @Transactional without readOnly and manually avoid updates
C. Annotate the method with @Transactional(readOnly = true) and avoid any save/update calls
D. Use @Transactional(readOnly = false) to allow updates if needed

Solution

  1. Step 1: Identify the goal of safe read-only data fetching

    The goal is to read data safely without accidental changes and improve performance.
  2. Step 2: Choose the best annotation and practice

    Using @Transactional(readOnly = true) explicitly marks the method as read-only, enabling optimizations and preventing writes.
  3. Final Answer:

    Annotate the method with @Transactional(readOnly = true) and avoid any save/update calls -> Option C
  4. Quick Check:

    Use readOnly = true for safe, optimized reads [OK]
Hint: Use readOnly = true to prevent accidental writes [OK]
Common Mistakes:
  • Skipping readOnly and risking accidental writes
  • Not using transactions at all
  • Using readOnly = false when no updates needed