4

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?

ChanceLai
  • 78
  • 1
  • 8
  • 1
    Where is the transaction managed? Why do you say that only C is saved, I would think that A and C would be eligible to be saved. saveB() is unreachable code. This example looks over-simplified to the point it's not really useful. Is this Java 7? They added try-with-resources in Java 7, so try/catch/finally blocks behave differently. – GlenPeterson Nov 13 '12 at 15:27
  • 1
    `throw RuntimeException` won't compile, did you by chance mean something like `throw new RuntimeException()`? – gnat Nov 13 '12 at 16:02
  • maybe you should add information about **RuntimeException will cause session to be broken and can't be used**, so the other people that didn't know the **Rollback** concept when exception, didn't confuse and think you didn't know the concept of try, catch, and finally. – Angga Jul 29 '13 at 04:09

1 Answers1

0

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:

A try statement with a finally block is executed by first executing the try block. Then there is a choice...

  • If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
    If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice...
    • If the catch block completes abruptly for reason R, then the finally block is executed...

In your code snippet, RuntimeException corresponds 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 e in your snippet corresponds to "reason R" in above quote.

Then, as required by language specification, "finally block is executed".

gnat
  • 6,213
  • 108
  • 53
  • 73
  • 3
    maybe the point of this question is not how try catch work. but how hibernate manage to throw runtime exception but still can save(and much weird it can commit). because runtime exception will break down the session. that will cause all the change to be rolled back, and cant be committed. – Angga Jul 29 '13 at 03:49