Explore foundational concepts of Spring Boot database transactions and error handling with these essential questions. This quiz covers transactional propagation, rollback strategies, exception types, annotation usage, and practical error management techniques for robust Spring Boot applications.
Which annotation is typically used to declare a transactional method in a Spring Boot service class?
Explanation: The @Transactional annotation is used to indicate that a method should run within a database transaction. The other options, such as @Persisted, @Connection, and @Database, are not valid transaction-related annotations in this context. Using @Transactional ensures that the method’s actions are atomic, consistent, isolated, and durable.
By default, a Spring Boot transaction rolls back when which type of exception is thrown?
Explanation: By default, transactions rollback on runtime exceptions or errors, helping avoid inconsistent database states. Checked exceptions do not cause a rollback unless explicitly specified. Only rolling back on specific exceptions like SQLSyntaxErrorException or null pointer exception is not the standard default behavior.
If a method declared as transactional calls another transactional method with propagation set to REQUIRES_NEW, what happens to their transactions?
Explanation: REQUIRES_NEW propagation starts a fresh transaction, suspending the outer transaction until the inner one finishes. Sharing the same transaction happens with the default (REQUIRED) propagation. Running without a transaction or pausing the outer transaction indefinitely are incorrect descriptions.
What is the purpose of setting 'readOnly=true' in the @Transactional annotation for a method that retrieves data?
Explanation: Setting 'readOnly=true' signals the database that data modification is not expected, possibly allowing transaction optimizations and safeguarding against accidental updates. It does not block data reads—rather, it still allows querying. It does not force updates or require a writable connection for reads, which would be unnecessary.
A service method annotated with @Transactional throws a custom checked exception by default. What happens to the transaction?
Explanation: By default, only runtime exceptions or errors trigger rollback, so if a method throws a checked exception with no additional configuration, the transaction is committed. Rollback is not always triggered for checked exceptions. Transaction suspension or application restart are unrelated actions and do not normally occur in this scenario.
How can you specify that a Spring Boot transaction should roll back on a specific checked exception?
Explanation: The rollbackFor attribute within @Transactional can be set to a checked exception class to trigger a rollback when that exception is thrown. Declaring exceptions in the throws clause does not affect transaction behavior. The readOnly attribute is unrelated, and @ExceptionHandler is used for handling, not transactional rollback.
Why might you specify an isolation level, such as Isolation.SERIALIZABLE, on a transactional method in Spring Boot?
Explanation: Isolation levels define how transaction integrity is maintained, preventing issues like dirty, non-repeatable, or phantom reads. Logging SQL or reducing memory are not purposes of isolation settings. Method readability is not affected by the isolation level configuration.
When a database constraint violation occurs in a Spring Boot application, which approach helps handle the exception and inform the user gracefully?
Explanation: Catching exceptions and providing clear, helpful feedback to users is a best practice for good user experience and maintainability. Silently ignoring errors leads to hidden bugs. Restarting the connection is unnecessary and inefficient. Displaying stack traces exposes internal details and is not user-friendly.
What is the primary use of the @ExceptionHandler annotation in a Spring Boot controller?
Explanation: @ExceptionHandler is used to define methods that capture specific exceptions thrown by controller methods, allowing for custom response messages. It is not intended for persistence logic, task scheduling, or monitoring transactions. Its main function is error handling within the web layer.
If a non-transactional method calls a transactional method, what happens to the transactional boundaries of the called method?
Explanation: A method annotated as transactional will start a transaction when invoked, even if the calling method is not transactional. No transaction would result in changes not being committed, which is incorrect. Both methods do not have to be transactional, and the outer method does not dictate the transaction unless it is transactional itself.