javaspringtransactionsspring-transactions

Spring Transactional without rollback


 @Transactional
 public void setSomething(String name) { ... }

Sorry to ask this very basic question, Spring @Transactional annotation is so powerful but yet super hard to understand.

Based on the code above, I don't have rollbackFor control, meaning, if there is exception, this transactional context will not be rollback. But based on my experience in old way to covering transaction block, if there is no rollback for exception, commit will be skipped and cause the (Oracle) database's table being locked (suspend, other user can't commit their SQL).

Will Spring have the same issue without using rollbackFor?


Solution

  • The rollbackFor and related parameters are for fine-tuning. If you omit them, the default behaviour is to rollback for RuntimeExceptions. Other exceptions don't perform a rollback and any changes made into the database will be committed.

    The transaction is either committed or rolled back, there's no way a @Transactional method will result in an unterminated transaction (at least in normal operation).