This fixes an issue where two transactions started around the same time could run separately instead of nesting, causing the statements from one to end up running not within a transaction
Rollback callbacks weren't being properly called, and some other things were in the wrong place, particularly with nested transactions.