One Service method invoke inner multiple method for Spring transaction

前端 未结 3 1222
情话喂你
情话喂你 2020-12-20 17:13
package com.bluesky;

public interface FooServiceIface {
    public  void insertA();
    public void insertB();
}

package com.bluesky         


        
相关标签:
3条回答
  • 2020-12-20 17:49

    KLE's answer is solid advice. Just to complete the picture, there is a workaround, but it breaks everything AOP stands for:

    public void insertA() {
        this.getJdbcTemplate().execute("insert student(name) values('stuA')");
        // this works, but... gah!
        ((FooServiceIface) AopContext.currentProxy()).insertB();
        int i=10/0;
    }
    

    Reference:

    • 7.6.1. Understanding AOP proxies
    • AopContext javadoc
    0 讨论(0)
  • 2020-12-20 18:03

    I understand your problem. There are some technically complex ways to get it work, but we don't usually consider they are worth it. I suggest another approach, simple and powerful, that actually improves your code.

    Instead of having them on the same Spring bean, have them on two different Beans, B being injected into A.

    I known I'm not answering your question, but think about the advantages :

    • Dead simple, it will cost you very little time now.
    • To require a new transaction, B is probably a different concern. Having a different concern is a different class seem exactly what we are looking for, good design.
    • You don't have anything new and complex to explain to your colleagues (or document for the future developers), it simplicity makes it immediately understandable.
    0 讨论(0)
  • 2020-12-20 18:05

    KLE's advice about refactoring your code is right on the money, but as for why it's not working, Spring AOP uses JDK dynamic proxies by default to provide AOP. That means that when you inject your service into something, what's really getting injected is a Proxy instance that implements your service interface. When a method is invoked on this proxy, it runs the transaction code before delegating to your actual service instance. Once the control flow is inside your service, though, calling another method via this.foo() (even if the this is implicit) just invokes a method on that same instance: your service. It doesn't go back to the proxy, which is the only thing that knows about transactions. If you switched to build- or load-time bytecode weaving with AspectJ, then you could do this, and it would work as expected, since the transaction-invoking code would be woven directly into your service code instead of living in a separate object.

    0 讨论(0)
提交回复
热议问题