Is it posibble to prevent the outer transaction context from rollback even after a exception that marked as rollBackFor thrown?
Clash Royale CLAN TAG#URR8PPP
Is it posibble to prevent the outer transaction context from rollback even after a exception that marked as rollBackFor thrown?
Suppose a user signup process like this:
@Service
public class UserService
@Resource
private EmailService emailService;
@Resource
private Jmstemplate jmsTemplate;
@Transactional(rollbackFor = Exception.class)
public void signUp(User user)
//save user to DB, etc
postSignUp(User user);
/**
* Business that not so important
* suppose this method may throw any exception
*
**/
public void postSignUp(User user)
emailService.sendEmail(user);
jmsTemplate.sendSignUpEvent(user);
...
We make the signUp() method as transactional. if any exception thrown within signUp() method, the transaction will rollback.
And of course, any exception thrown within postSignUp() will also result in the rollback of the transaction.
But, since the logic in postSignUp() was not so important, I wonder how can I prevent the outer transaction from rollback even a exception was thrown within the postSignUp() method?
Thanks.
postSignUp(User user);
try/catch
It is feasible. Any other elegant way except use
try/catch
?– Wuaner
Jul 4 at 1:16
try/catch
True, exception handling isn't particularly elegant, but it is mandatory for clean code.
– DamCx
Jul 4 at 6:10
1 Answer
1
Have you tried noRollbackFor
property of Transactional
?
noRollbackFor
Transactional
It goes like:
@Transactional(noRollbackFor=SampleCustomException.class)
But postSignUp() may throw any exception, and I don't like surrounding the whole method with try/catch and re-throw it as a custom exception.
– Wuaner
Jul 4 at 1:14
This is a clean way of coding, though
– DamCx
Jul 4 at 6:09
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You possibly could surround your
postSignUp(User user);
call with atry/catch
block to handle any exception during that call. Question is more like if the user can't log in, is it worth recording the event?– DamCx
Jul 3 at 10:03