开发者

Method forwarding on methods which contain Spring's @Transactional annotation?

I have a service implementation which looks sort of like the following:

class FooImpl implements Foo {

   @Override
   @Transactional(...)
   public void doSomething(String baz) {
       return this.doSomething(Arrays.asList(new String[] { baz }));
   }


   @Override
   @Transactional(...)
   public void doSomething(List<String> bazList) {
        // do something crazy
        return null;
   }

}

I want to understand, what is happening here? I'm calling a method with the @Transactional annotation from ano开发者_如何学编程ther method with the @Transactional annotation (method forwarding)...

My question:

Is what I have above creating any strange situations? It seems to be working correctly, so I think that the outer transaction is being used and a new one is not being created. Is this correct? Also, any tips on how I could verify/debug this?


It is a typical situation. @Transactional has a propagation property. This property defines the behaviour in case when a @Transactional method is invoked - whether a new transaction is needed, whether an existing transaction is needed, etc.

By default the propagation is REQUIRED, which means - a new transaction will be started if none exists; otherwise the existing will be used.

Read here about transaction propagation.

Another thing to mention here is that (in some configuration scenarios) in case you are calling methods of the same class, the transaction interceptor is not triggered, and so the propagation is not taken into account at all. So in your case, it is most likely that the @Transactional on the 2nd method is ignored.


Assuming you're using JDK proxies:

The second @Transactional will never even be invoked. When you use JDK proxies for AOP, the advice only gets applied to the method when the call is made against the proxied bean. When you call another method on the same class, the call isn't passing through the code that spring 'wraps' your method in when it creates the bean. If you needed that to happen you would have to ask the ApplicationContext for a reference to the bean proxy that it created.

As an aside, Collections,singletonList !

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜