Spring transaction rollback

  spring

Spring Default Non-nested Calls

The Spring Framework’s transaction infrastructure code will only identify transaction rollbacks by default when runtime and unchecked exceptions are thrown. That is, when an instance of a RuntimeException or its subclass is thrown. (Errors, too-by default-identifies transaction rollback. ) Checked exceptions thrown from the transaction method will not be identified for transaction rollback.

Service nested calls



//service A本身没有对DAO的处理,但是B和C就有对DAO的处理
service A {

service B.someMethod()//调用service B

service C.someMmethod()//调用service C

}

1. As long as the checked exception is thrown, it will not be rolled back regardless of whether it is captured or not.
2. If C throws a runtime exception and A catches it, B will not roll back; If A does not capture, rollback.

Rollback only

org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only。



begin transaction;

for(……)

{

    doSomething();//抛出了异常或者明确设置了事务为RollbackOnly

}

commit;

end;



doSomething()伪代码:

try

{

          begin transaction;

          /**  do anything  */

          commit;

          end;

}

catch()

{

}

In spring, we have configured the transaction propagation mechanism to be REQUIRED, so the two transactions will eventually merge into one transaction. In doSomething () in the program, an exception is thrown due to some reason (or the transaction is explicitly set to RollbackOnly), but since the exception has been caught inside, it will not affect the continued execution of the external for loop. when the external for loop continues to execute and is ready to commit the transaction, When it is found that the status bit of this transaction has been set to RollbackOnly, spring will throw a transaction action rollback-only.

docs