@Transactional(rollbackFor = Exception.class)
and @Transactional()
are annotations used for transaction management in the Spring framework, and they have the following differences:
-
@Transactional(rollbackFor = Exception.class)
:- The parameter is used
rollbackFor
to specify that the transaction rollback occurs when an exception of the specified type is encountered. rollbackFor = Exception.class
Indicates that transaction rollback will be triggered when any type of exception is encountered.- This means that no matter what type of exception is encountered, including runtime exceptions and checked exceptions, a transaction rollback will be triggered.
- The parameter is used
-
@Transactional()
:- The parameter is not specified , so by default a transaction rollback will
rollbackFor
only be triggered if a runtime exception ( ) or an exception of type Error is encountered .RuntimeException
- For checked exceptions (Checked Exception), transaction rollback is not triggered by default.
- If a checked exception is thrown within the method body, the transaction will continue to commit rather than rollback.
- The parameter is not specified , so by default a transaction rollback will
Summarize:
@Transactional(rollbackFor = Exception.class)
The rollback strategy is specified, no matter what type of exception is encountered, the transaction rollback will be triggered.@Transactional()
By default, transaction rollback is only triggered when a runtime exception or Error type exception is encountered, and rollback is not triggered by default for checked exceptions.
In practical applications, according to business requirements and exception handling strategies, you can choose to use appropriate @Transactional
annotations to manage transactions and specify appropriate rollback behaviors.