@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?
The default <tx:advice/> / @Transactional settings are:
REQUIRED.DEFAULT.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With