I have this:
try(Transaction t = sess.beginTransaction()){
// do something with t...
} catch (Exception e) { // abort transaction if something goes wrong
...
t.rollback();
t.close();
}
However, t is not in scope of the catch block. I reviewed this here try/catch scope in Java/C#.
Declaring t before the try-catch-block doesn´t work, as the syntax in try requires the variable to be declared right there.
Is there any way that I can initialize t like this, and still make sure that the transaction gets closed / rollbacked? Or should I just begin the transaction before the try/catch-block?
EDIT: Transaction is an 'AutoClosable'. Does this mean I don't need to explicitly handle rollback and closing?