Spring Certified Professional 2023 · Free Practice Question Medium
Question 2
Select two essential components for enabling transaction management in Spring. (select 2)
-
A
Add
@EnableTransactionManagement to a Java configuration class. -
B
A class must incorporate the
TransactionInterceptorinterface to facilitate transactions. -
C
Annotate a class, an interface, or individual methods requiring a transaction with the
@Transactionalannotation. -
D
A class must be annotated with
@Serviceand@Transaction. -
E
Develop a Spring AOP advice to establish transactional functionality.
Reveal correct answers
Correct answers: A, C
Explanation
To use transactions in Spring, the two required elements from the options you’ve provided are:
Annotate a class, an interface, or individual methods requiring a transaction with the @Transactional annotation.
Add @EnableTransactionManagement to a Java configuration class.
The @Transactional annotation is used to declare that a method, class, or interface should have transactional behavior. The @EnableTransactionManagement annotation enables Spring’s annotation-driven transaction management capability, similar to the support found in Spring’s <tx:*> XML namespace.
The other options are not correct:
A class does not need to implement the
TransactionInterceptorinterface to require a transaction.Writing a Spring AOP advice is not a requirement as transactional behavior can be added declaratively with the
@Transactionalannotation.A class does not need to be annotated with both
@Serviceand@Transaction. The@Serviceannotation is a stereotype for the service layer, and there is no@Transactionannotation in Spring. Transactional behavior is typically declared with@Transactional.
Using @Transactional:
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
