Custom annotation labels for @Transactional do not work

I am trying to create custom annotations for a shortcut, as stated in the documentation:

@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Transactional("order") public @interface OrderTx { } 

However, when I comment on methods with custom annotation, I get an exception:

There is no hibernation session associated with the thread, and the configuration does not allow you to create ...

etc .. Although annotating a method using @Transactional works fine.

Since the method I annotate does not belong to the Bean created from the application context, I assume that AnnotationTransactionAspect does not work with custom annotations, and the AOP magic does not work.

How can I get custom annotations that reduce transactions and work everywhere?

Did I miss something else?

+6
source share
1 answer

Here are the points used in AnnotationTransactionAspect :

 /** * Matches the execution of any public method in a type with the * Transactional annotation, or any subtype of a type with the * Transactional annotation. */ private pointcut executionOfAnyPublicMethodInAtTransactionalType() : execution(public * ((@Transactional *)+).*(..)) && @this(Transactional); /** * Matches the execution of any method with the * Transactional annotation. */ private pointcut executionOfTransactionalMethod() : execution(* *(..)) && @annotation(Transactional); 

I would say that it is pretty clear that meta annotations are not mapped (and I don’t even think that there is a valid pointcut pointcut that can catch meta annotations). So I think you will have to subclass AbstractTransactionAspect and provide your own implementation for this pointcut to catch your custom annotation:

 /** * Concrete subaspects must implement this pointcut, to identify * transactional methods. For each selected joinpoint, TransactionMetadata * will be retrieved using Spring TransactionAttributeSource interface. */ protected abstract pointcut transactionalMethodExecution(Object txObject); 
+2
source

Source: https://habr.com/ru/post/888843/


All Articles