Deal with connection close (client- or server-initiated) in transaction.atomic()

See Trac tickets #21202 and #21239.