4.4.5.3.1. Rollback of a Nested Transaction
If a nested transaction was created via getTransaction()
and rolled back, then commit of the enclosing transaction will be impossible. For example:
void methodA() {
Transaction tx = persistence.createTransaction();
try {
// (1) calling a method creating a nested transaction
methodB();
// (4) at this point an exception will be thrown, because transaction
// is marked as rollback only
tx.commit();
} finally {
tx.end();
}
}
void methodB() {
Transaction tx = persistence.getTransaction();
try {
// (2) let us assume the exception occurs here
tx.commit();
} catch (Exception e) {
// (3) handle it and exit
return;
} finally {
tx.end();
}
}
If the transaction in methodB()
is created with createTransaction()
instead, then rolling it back will have no influence on the enclosing transaction in methodA()
.