Reputation: 33
I cannot delete from DB if I throw an exception immediately on the next line.
public void verifyExpiration(RefreshToken token) {
if (token.getExpiryDate().compareTo(Instant.now()) < 0) {
this.delete(token.getToken());
throw new TokenException("Refresh token was expired: " + token.getToken());
}
}
public void delete(String token) {
this.refreshTokenRepository.deleteByToken(token);
}
What am I missing?
Upvotes: 2
Views: 781
Reputation: 4582
In SpringBoot if you want to complete the transaction while exception is also thrown you can use noRollbackFor
for @Transactional
annotation as below:
@Transactional(noRollbackFor=TokenException.class)
Upvotes: 2
Reputation: 45
Spring repositories are already annotated with @Transactional
. Which means if an error is thrown while proccessing your data all the flow would be rollbacked.
If you want to disable the trnasactional process of the spring repository you need to redifine the delete
method in your interface and annotate it with @Transactional(propagation = Propagation.SUPPORTED)
check here
Upvotes: 0