rollbackfor in exception 和 runtimeException 应该怎么选择?

What should I choose between rollbackfor in exception and runtimeException?

当我在异常中使用事务时,我使用@Transactional(rollbackFor = Exception.class).

但是,当我使用 RuntimeException 时,我使用 @Transactional。

rollbackfor in exception和runtimeException应该怎么选择?

例如) public class BadRequestException 扩展异常 -> public class BadRequestException 扩展了 RuntimeException

Spring 提供与 EJB 相同的回滚到 RuntimeException 的内置支持。虽然,添加对应用程序异常(已检查异常)的回滚支持,您可以这样定义。

@Transactional(rollbackFor = BadRequestException.class)

Spring 文档:"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