Fix regression on after_commit in nested transactions. (master)

related to #16432 .

[fixes #16425]

This is practically a backport of that fix, plus 1 commit to make it work.

This is to ensure the same behaviour as 3.2
Hm, I just spend a few minutes reading this and I still wasn't 100% sure if these changes would actually result in the same behaviour in all cases.. this code is confusing enough as is, so personally I prefer to keep it more explicit here (and you still need `transaction.rollback` in the second rescue block anyway, so that just adds to the confusion imo), wdyt?