Try   HackMD

@Transactional rollback rule

tags: spring,transaction,rollback

only unchecked exception will trigger rollback in default.

According to Spring documentation:

In its default configuration, the Spring Framework’s transaction infrastructure code marks a transaction for rollback only in the case of runtime, unchecked exceptions. That is, when the thrown exception is an instance or subclass of RuntimeException. ( Error instances also, by default, result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

unchecked exception(runtime exception)

1. runtimeException::non-checked excetion => trigger rollback, in class declaration, you dont need add throws exception and compile successes.
2. java dont check these exception during compile.

checked exception

1.other exception not in runtimeexception::checked exception => non-trigger rollback by default, in class declaration, you need add throws exception and compile successes.
2. java check these exception during compile.

if want to rollback for all exception in transaction:

@Transactional(rollbackFor = Exception.class)