Junior — Middle
In what situations will using transactions not lead to automatic rollback of changes?
sobes.tech AI
Answer from AI
Transactions in Java (for example, using Spring Transaction Management) will not automatically roll back in the following situations:
- The exception is not unchecked (RuntimeException) or Error, but a checked exception, and rollback is not explicitly specified in the settings (rollbackFor).
- The exception was caught and handled within the transactional method, i.e., it did not propagate outward.
- The transaction was configured with the
noRollbackForparameter for this type of exception. - The transaction has already been committed before the error occurred.
Example:
@Transactional
public void someMethod() {
try {
// database operations
} catch (Exception e) {
// exception handled, transaction will not rollback automatically
}
}
In this case, if the exception is caught, it will not cause an automatic rollback.