Consideration of a general use case for a user creating a new account in a web application, and an application sending a confirmation email to a userβs address. From what I saw, this is usually implemented in one of three ways:
- The web controller calls the service method, which creates a user account and sends the email as part of a single transaction.
- The web controller calls the service method (with tx = never propagation), which calls the 1st method to create the user account in the transaction, and then calls the second method to send the email.
- The web controller calls the 1st service method, which creates the user account in the transaction, and then the second service method, which sends the email.
The 1st approach is simple and simple, but there is a risk that the transaction will be canceled after sending the email message, thereby invalidating the message. The second approach is more complex, but it ensures that the letter is sent only if the userβs creation really succeeded. The 3rd approach is simple, but burdens the web tier with business logic that he does not need to know about.
Is there a simpler approach, possibly managed by AOP, that ensures that an email will only be sent if the user create transaction really succeeds? Am I paranoid in thinking that the first approach may fail?
We use the Java EE + Spring stack and are ready to integrate additional APIs (AOP? Spring Integration?) To achieve this.
Hooray!
source share