The project used Spring + Hibernate
Sample code:
public void method(){
try{
dao.saveA(entityA);
//condition may be throw RuntimeException;
dao.saveB(entityB);
}catch(RuntimeException e){
throw e;
}finally{
dao.saveC(entityC)
}
}
Finally, just entityC will be saved in database in test.
I think saveA, saveB, saveC in the same transaction,they should not be committed.
In this case, I want to know why entityC is committed.
How does Spring do this in the finally block?
//————————-//
Actually, my question is that:how spring ensure the transaction committed in the finally block .
will Spring be get a new connection in the finally block?
Spring has nothing to do with this, what you obtain is a behavior as required by JLS 14.20.2 Execution of try-catch-finally:
In your code snippet,
RuntimeExceptioncorresponds to “value V” in above quote.Exception is thrown and caught in respective catch block, which then “completes abruptly” by re-throwing the exception – that is,
throw ein your snippet corresponds to “reason R” in above quote.Then, as required by language specification, “
finallyblock is executed”.