问题
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?
Thanks.
来源:https://stackoverflow.com/questions/62875839/transactional-annotation-to-rollback-for-every-checked-exception-with-aspectj