# Buggy code
def set_isolation_level(self, connection, level):
    level = level.replace("_", " ")
    if level not in self._isolation_lookup:
        raise exc.ArgumentError(
            "Invalid value '%s' for isolation_level. "
            "Valid isolation levels for %s are %s"
            % (level, self.name, ", ".join(self._isolation_lookup))
        )
    cursor = connection.cursor()
    cursor.execute("SET TRANSACTION ISOLATION LEVEL %s" % level)
    cursor.close()


# Traceback (pyodbc.ProgrammingError)
from sqlalchemy import create_engine
engine= create_engine("ConnectionUrl")
conn = engine.connect()
conn = engine.connect()
conn.execution_options(
...                     isolation_level="SNAPSHOT")
<sqlalchemy.engine.base.Connection object at 0x000002269FE28F28>
conn.execute("SELECT TOP 10 * FROM Staging.CoreRespdet")
Traceback (most recent call last):
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 1236, in _execute_context
cursor, statement, parameters, context
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\default.py", line 536, in do_execute
cursor.execute(statement, parameters)
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Transaction failed in database 'ETL' because the statement was run under snapshot isolation but the transaction did not start in snapshot isolation. You cannot change the isolation level of the transaction to snapshot after the transaction has started unless the transaction was originally started under snapshot isolation level. (3951) (SQLExecDirectW)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 974, in execute
return self._execute_text(object_, multiparams, params)
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 1147, in _execute_text
parameters,
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 1240, in _execute_context
e, statement, parameters, cursor, context
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 1458, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\util\compat.py", line 296, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\util\compat.py", line 276, in reraise
raise value.with_traceback(tb)
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\base.py", line 1236, in _execute_context
cursor, statement, parameters, context
File "C:\Users\*****\source\repos\*****\ETLApp_Dev\ETLApp\ETLAppEnv_3_7\lib\site-packages\sqlalchemy\engine\default.py", line 536, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Transaction failed in database 'ETL' because the statement was run under snapshot isolation but the transaction did not start in snapshot isolation. You cannot change the isolation level of the transaction to snapshot after the transaction has started unless the transaction was originally started under snapshot isolation level. (3951) (SQLExecDirectW)") [SQL: 'SELECT TOP 10 * FROM Staging.CoreRespdet'] (Background on this error at: http://sqlalche.me/e/f405)


# Fixed code
def set_isolation_level(self, connection, level):
    level = level.replace("_", " ")
    if level not in self._isolation_lookup:
        raise exc.ArgumentError(
            "Invalid value '%s' for isolation_level. "
            "Valid isolation levels for %s are %s"
            % (level, self.name, ", ".join(self._isolation_lookup))
        )
    cursor = connection.cursor()
    cursor.execute("SET TRANSACTION ISOLATION LEVEL %s" % level)
    cursor.close()
    if level == "SNAPSHOT":
        connection.commit()