256
256

Reputation: 33

JPA delete does not executed

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

Answers (2)

Gaurav Jeswani
Gaurav Jeswani

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

Kais Neffati
Kais Neffati

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

Related Questions