Consider the following two blocks of Java psedo-code in a system that uses optimistic transactions.
Example A:
try {
txn.begin();
// database operations
txn.commit();
}
catch (Exception e) {
txn.rollback();
}
Example B
txn.begin();
// database operations
try {
txn.commit();
}
catch (Exception e) {
txn.rollback();
}
I'm seeing transactions being conducted both ways in our code; I'm sure that A is correct. my intuition tells me that B is wrong, but it seems that there is no harm in B since the commit()
is in the try block, and can be caught and rolled-back in the case of an error. Please explain whether B is correct, and why. Thanks!
Edit: So I'm not really getting the answer I'm looking for. I already know that B is somehow "bad", what I'm looking for is why it is bad; that is, is there some possible situation where A would work where B would fail?
-tjw