Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 882
Sam YC Avatar asked Aug 13 '26 12:08

Sam YC


1 Answers

The default <tx:advice/> / @Transactional settings are:

  • Propagation setting is REQUIRED.
  • Isolation level is DEFAULT.
  • Transaction is read/write.
  • Transaction timeout defaults to the default timeout of the underlying
  • Transaction system, or none if timeouts are not supported.
  • Any RuntimeException triggers rollback, and any checked Exception does not.

So in your case this will be rollbacked if you will have a RuntimeException.

But usually it is not sufficient to tell you simply to annotate your classes with the @Transactional annotation, add @EnableTransactionManagement to your configuration.

You can configure exactly which Exception types mark a transaction for rollback, including checked exceptions. The following XML snippet demonstrates how you configure rollback for a checked, application-specific Exception type.

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="get*"
                   read-only="true"
                   rollback-for="NoProductInStockException"/>
        <tx:method name="*"/>
    </tx:attributes>
</tx:advice>

Or with the annotation:

@Transactional(rollbackFor = NoProductInStockException.class)

Detailed documentation you can find in the official Spring documentation:
šŸ‘‰ Introduction to Spring Framework transaction management

Hope that helps.

like image 112
Patrik Bego Avatar answered Aug 16 '26 02:08

Patrik Bego



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!