Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a method which is annotated with @Transactional and another custom annotation @Custom. This custom annotation is wrapped around an advice. The sequence of operation is like below:

1.Transactional method gets called
2.Sees @Transactional and @Custom
3.As @Custom is intercepted by around advice, it first executes code before invocation of method
4.invocationContext.proceed()
5.Transaction gets created
6.Actual method runs
7.Back to around advice and executes code after method invocation

I want to create the transaction before the advice is called. Like below:

1.Transactional method gets called
2.Sees @Transactional and @Custom
3.Transaction gets created (propagate this transaction to @Custom)
4.As @Custom is intercepted by around advice, it first executes code before invocation of method
5.invocationContext.proceed()
6.Actual method runs
7.Back to around advice and executes code after method invocation

So that both advice and method are in same transaction

Can we use @Order on @Transactional, so taht first my transacion is created and then the advice executed?

Yes, in the @Configuration class use:

@EnableTransactionManagement(order = Ordered.HIGHEST_PRECEDENCE)

or whatever order you need

I guess @EnableTransactionManagement(order = Ordered.HIGHEST_PRECEDENCE) is the global config.

If you want @Transactional just on this method A(), not for other methods, maybe you can define method B() to call A(). This way will point out the order of annotations.

Like this:

// I think this is not a smart solution. 
// If you have a good idea, please let me know.
@Service
class TestB {
    @Autowired
    private TestA testA;
    @Transactional
    public void b() {
        // !! notice: you should call bean testA that will use spring's AOP
        testA.a();
@Service
class TestA {
    @Custom
    public void a() {}
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.