Fixed #21239 -- Maintained atomicity when closing the connection.
Wrapped exceptions in _set_autocommit. Refs #21202.
Fixed #21202 -- Maintained atomicity when the server disconnects.

Thanks intgr for the report.

This commit doesn't include a test because I don't know how to emulate a
database disconnection in a cross-database compatible way.