Denorm
Denorm

Reputation: 466

Change the default settings for @Transactional in Spring

Currently, we have to add rollbackFor=Exception.class for 90% of the methods in our application that use @Transactional annotation. I was wondering if there is a way to set the default for rollbackFor to Exception.class somewhere so that we don't have to explicitly specify it every time.

Upvotes: 1

Views: 1101

Answers (1)

shazin
shazin

Reputation: 21883

Since it is an annotation, the standard is writing your own custom annotation and replacing it with @Transactional(rollbackFor = Exception.class). If you annotate with the following annotation you don't need to specify rollbackFor in everywhere because it will be implied.

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

}

Upvotes: 5

Related Questions