[ACCEPTED]-Spring @Transactional Annotation : Self Invocation-self-invoking-function

Accepted answer
Score: 18

What I don't understand is why do people 21 say self invocation breaks transaction?

I 20 never heard that self-invocation breaks 19 transaction. All I know is that self-invocation 18 will not start a new transaction and you 17 already mentioned the reason why.

Snippet from Spring's Transaction Management Specification

Note In proxy 16 mode (which is the default), only external 15 method calls coming in through the proxy 14 are intercepted. This means that self-invocation, in 13 effect, a method within the target object 12 calling another method of the target object, will 11 not lead to an actual transaction at runtime 10 even if the invoked method is marked with @Transactional.

If 9 you remove @Transaction annotation from saveAB(), you would 8 observe that method saveA() and saveB() would not run 7 under transaction even though it is annotated 6 with @Transactional. However, if you call saveA() or saveB() from outside 5 the class, it will run under transaction 4 as expected. That is the reason why people 3 advice to be cautious with self-invocation.

public void saveAB(A a, B b)

public void saveA(A a)

public void saveB(B b)

In 2 my view, self-invoking any public method 1 is a bad idea.

Score: 1

If you call saveAB and saveB throws an Exception, your transaction 3 will rollback.

Self-invocation doesn't break 2 the transnational context. It just not starts 1 one if nk contdxt id available.

More Related questions