Replace in_transaction with try/except to support older Python versions.