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 TransactionInterceptor interface to facilitate transactions.

  • C

    Annotate a class, an interface, or individual methods requiring a transaction with the @Transactional annotation.

  • D

    A class must be annotated with @Service and @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:

  1. Annotate a class, an interface, or individual methods requiring a transaction with the @Transactional annotation.

  2. 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 TransactionInterceptor interface to require a transaction.

  • Writing a Spring AOP advice is not a requirement as transactional behavior can be added declaratively with the @Transactional annotation.

  • A class does not need to be annotated with both @Service and @Transaction. The @Service annotation is a stereotype for the service layer, and there is no @Transaction annotation in Spring. Transactional behavior is typically declared with @Transactional.


https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html


Using @Transactional:

https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/annotations.html

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need