1

I would like to use @Transactional annotation (org.springframework.transaction.annotation) for transactions management. I need the rollback to occur for every exception (not just unchecked exceptions), so I use:

@Transactional (rollbackFor = Exception.class)

I use AspectJ mode, and it's working fine.

The problem is, I don't want the developer to have to add the rollbackFor attribute every time. I found this answer, that suggests to extend the @Transactional annotation, like so:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional(rollbackFor=Exception.class)
public @interface TransactionalWithRollback {
}

and use the new annotation instead of the original one.

However, this approach is not working for me. I don't see that the AjcClosure files are created when I build the project, for those methods that were annotated with the extended annotation. They are created as expected for methods annotated by the original Transactional annotation.

Does this solution doesn't work with AspectJ? Possibly AspectJ handles only the original Transactional annotation? Is there another solution for my problem, that can work with AspectJ so that we don't have to specify the rollbackFor attribute every time?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dikla
  • 3,461
  • 5
  • 30
  • 43
  • Did you check solutions in https://stackoverflow.com/questions/3607803/howto-extend-spring-annotation-transactional ? – Ori Marko Jul 13 '20 at 12:37
  • Looks like what I need, I will check. Thanks! – Dikla Jul 15 '20 at 06:38
  • 1
    Does this answer your question? [Spring: automatic rollback on checked exceptions](https://stackoverflow.com/questions/44898884/spring-automatic-rollback-on-checked-exceptions) – jhyot Mar 26 '21 at 09:19

0 Answers0