Small bugs appear, so be alert

异常Transaction rolled back because it has been marked as rollback-only  

 

 
 
The following exception suddenly appeared in the system today:

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

Then I traced it to the code and looked at it. The pseudo code is roughly as follows:
begin transaction;
for(...)
{
doSomething();//An exception is thrown or the transaction is explicitly set to RollbackOnly } commit; end; doSomething() pseudo Code: try { begin transaction;           /** do anything */ commit;           end; } catch() { } Analysis Reason: This is an example of a transaction nested transaction. In spring, we have configured the transaction propagation mechanism to be REQUIRED    







         


         







, so the two transactions will eventually merge into one transaction. In the program doSomething() throws an exception for a certain reason (or explicitly sets the transaction to RollbackOnly), but because the exception has been caught internally, it will not affect the continuation of the outer for loop, when the outer for When the loop continues to execute and is ready to commit the transaction, it is found that the status bit of the previous transaction has been set to RollbackOnly. At this time, spring will throw a transaction rolled back because it has been marked as rollback-only.
Solution: I remove the things in the main method, the things that call the method remain unchanged, and form a partial return of things.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327060625&siteId=291194637