Spring @Transactional Annotations: Self Invocation

I know when a transaction method is called from one class, it will not be run in a transaction. Spring creates proxies for transactional methods and wraps them in a try-catch block and rolls back if an exception occurs. Consider the following scenario:

@Transactional
public void saveAB(A a, B b)
{
    saveA(a);
    saveB(b);
}

@Transactional
public void saveA(A a)
{
    dao.saveA(a);
}

@Transactional
public void saveB(B b)
{
    dao.saveB(b);
}

Suppose saveAB is called from another object, and an exception occurred in saveB, so saveA completed successfully, but saveB did not. As far as I know, although saveA and saveB are not transactional (because they are called from the same object), since saveAB is transactional, it still needs to be rolled back.

, , - , , ? , , , ? -, ?

+4
2

, , - , , ?

, . , , , , .

Spring

( ) -, . , , , , , @Transactional.


@Transaction saveAB(), , saveA() saveB() , @Transactional. , saveA() saveB() , , . .

public void saveAB(A a, B b)
{
    saveA(a);
    saveB(b);
}

@Transactional
public void saveA(A a)
{
    dao.saveA(a);
}

@Transactional
public void saveB(B b)
{
    dao.saveB(b);
}

, - .

+7

AB B , .

Seld-invocation , saveAB @Transactional.

, .

0

Source: https://habr.com/ru/post/1542463/


All Articles