使用MARS Connection时,连接正忙于另一个命令的结果

时间:2019-01-14 22:16:15

标签: python sql-server django

在Django中尝试使用以下命令进行dumpdata时:

python manage.py dumpdata --all --output /data/django_dump.json --verbosity 3

我收到以下错误:

CommandError: Unable to serialize database: ('HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server]Connection is busy with results for another command (0) (SQLExecDirectW)')
Exception ignored in: <generator object _cursor_iter at 0x7f1c4112b0c0>
Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/sql_server/pyodbc/compiler.py", line 133, in _cursor_iter
    cursor.close()
  File "/opt/conda/lib/python3.7/site-packages/sql_server/pyodbc/base.py", line 500, in close
    self.cursor.close()
pyodbc.ProgrammingError: The cursor's connection has been closed.

Following the advice of this post,这是我的数据库配置:

DATABASES = {
    'default': {
        'NAME': ...,
        'ENGINE': 'sql_server.pyodbc',
        'HOST': ...,
        'USER': ...,
        'PASSWORD': ...,
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'MARS_Connection': True,
        }
    }
}

使用MARS连接似乎完全没有帮助。我的配置错了吗?还有其他原因可能不起作用吗?

Python> = 3.6,Django> = 2,使用MS SQL Server(不确定哪个版本,没有太旧的版本)和conda-forge中的django-pyodbc-azure。

1 个答案:

答案 0 :(得分:0)

我尝试设置MARS_Connection参数,这终于起作用了(Django 3.0,django-mssql-backend 2.8.1),即,将'extra_params': 'MARS_Connection=Yes'放在选项中:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST'),
        'PORT': os.getenv('DB_PORT'),
        'TEST': {
            'NAME': os.getenv('DB_NAME'),
        },
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'extra_params': 'MARS_Connection=Yes'
        },
    }
}