Move transaction handling into context managers for fault tolerance.