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 another 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.
@Transactionalhas apropagationproperty. This property defines the behaviour in case when a@Transactionalmethod 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
@Transactionalon the 2nd method is ignored.