k.sh
k.sh

Reputation: 5

What should I choose between rollbackfor in exception and runtimeException?

When I use transaction in Exception, I use @Transactional(rollbackFor = Exception.class).

But, When I use RuntimeException, I use @Transactional.

What should I choose between rollbackfor in exception and runtimeException?

ex) public class BadRequestException extends Exception -> public class BadRequestException extends RuntimeException

Upvotes: 0

Views: 261

Answers (1)

lucid
lucid

Reputation: 2890

Spring provides inbuilt support to rollback to RuntimeException same as EJB. Though, adding rollback support for Application Exception (checked Exception) you can define like this.

@Transactional(rollbackFor = BadRequestException.class)

Spring documentation : "While the Spring default behavior for declarative transaction management follows EJB convention (roll back is automatic only on unchecked exceptions), it is often useful to customize this". source

Upvotes: 1

Related Questions