Why @Transactional rollbacks only unchecked exceptions?

What’s the idea behind this? Why not to rollback for all the exceptions by default?

1

From the javadoc of DefaultTransactionAttribute:

/**
 * The default behavior is as with EJB: rollback on unchecked exception
 * ({@link RuntimeException}), assuming an unexpected outcome outside any
 * business rules. Additionally, we also attempt to rollback on {@link Error} which
 * is clearly an unexpected outcome as well. By contrast, a checked exception is
 * considered a business exception and therefore a regular expected outcome of the
 * transactional business method, i.e. a kind of alternative return value which
 * still allows for regular completion of resource operations.
 * <p>This is largely consistent with TransactionTemplate's default behavior,
 * except that TransactionTemplate also rolls back on undeclared checked exceptions
 * (a corner case). For declarative transactions, we expect checked exceptions to be
 * intentionally declared as business exceptions, leading to a commit by default.
 * @see org.springframework.transaction.support.TransactionTemplate#execute
 */

So likely 1) to allow for easier migration from EJB, and 2) because checked exceptions are assumed to be “regular” behaviour and thus handled explicitly by the business code.

Simple answer:

Checked exceptions are those exceptions which needs to be handled by caller or just in case needs to be thrown by the keyword throws if not able to handle. These exceptions are verified at the compilation time.

But in case of unchecked exceptions, these are Error and RuntimeException exceptions that a caller is not able to handle because this is something that happens at run time.

Hence, by this definition, @Transactional will not handle checked exception as it’s the responsibility of the caller to handle. But the runtime exception is something that can’t be handled by the caller. So, in the case of any database query execution error happened via DAO/Service layer might lead to corruption of data in the database and we must need to rollback as this is sort of a runtime error that can’t be handled by caller gracefully. So, that’s why @Transactional only handles unchecked exceptions.

The @Transactional annotation in Spring rolls back only on unchecked exceptions by default, while checked Exceptions do not trigger a rollback unless explicitly configured. This behaviour is designed to align with the different meanings typically attributed to checked and unchecked exceptions in Java.

Unchecked Exceptions

  • Unchecked exceptions usually represent scenarios like null pointer dereferences, illegal arguments, or other logic errors that are outside of the normal flow of control. These are typically programming bugs or unexpected runtime conditions that should not happen during normal operation.

  • When such an error occurs, it’s safe to assume that the transactional work up to that point is invalid, so a rollback should happen by default.

Checked Exceptions

  • Checked exceptions are intended to signal recoverable issues, such as file not found, database connection failure, or business-related conditions (such as validation errors or user-specific errors).
  • Since checked exceptions indicate problems that might be handled gracefully, Spring assumes that the transaction might be recoverable and leaves it up to the developer to decide whether or not a rollback should occur. This gives more flexibility to the developer, as they can choose to retry or handle the exception without rolling back the transaction.

Explicit Rollback for Checked Exceptions

  • If you want a rollback to occur for a checked exception, you can explicitly specify this using the rollbackFor attribute of @Transactional.

    @Transactional(rollbackFor = { Exception.class })
    public void <method-name>() throws Exception {
      // logic here
    }
    
  • This overrides the default behaviour and forces a rollback even for checked exceptions.

Performance Considerations

  • Rolling back a transaction can be resource-intensive (such as database operations like undoing inserts/updates). Rolling back for recoverable or manageable checked exceptions could lead to unnecessary overhead in terms of performance and resource usage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật