4

While trying to dumpdata in Django with the following command:

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

I get the following error:

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, here's my database configuration:

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

Using the MARS connection doesn't seem to help at all. Is my configuration wrong? Is there some other reason this might not work?

Python>=3.6, Django>=2, using MS SQL Server (not sure which version, nothing too old) and django-pyodbc-azure from conda-forge.

scnerd
  • 5,836
  • 2
  • 21
  • 36
  • did you find any solution? – r4v1 Mar 22 '19 at 06:20
  • @prof.phython yeah, to not use SQL Server. I wish I could give better news, but at some point I just gave up trying to use MSSS with Python and switched to Postgres, and I don't think I figured this one out before making that switch. – scnerd Mar 22 '19 at 17:25
  • 5
    Coming in _much_ later to this, but `'MARS_Connection': True` isn't technically correct (should be `'Yes'`), but this also never worked for me. I had to specify `'extra_params': 'MARS_Connection=Yes'`. Hope this helps someone – Will Gordon Feb 22 '20 at 15:59
  • @WillGordon you helped me a lot. Thanks! :) – Shondeslitch Apr 23 '20 at 15:23

1 Answers1

6

I tried around setting the MARS_Connection parameter and this is what finally worked (Django 3.0, django-mssql-backend 2.8.1) i.e. putting 'extra_params': 'MARS_Connection=Yes' inside of the OPTIONS:

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'
        },
    }
}
lhaferkamp
  • 676
  • 8
  • 10