Question

I have some strange issue with querying SQL Server from django.

When I query db twice in single request, I got errors in some cases. Namely when first db query returns big amount of data, we end up with error while querying db second time.

Details:

We're using Microsoft SQL Server backend for Django (https://bitbucket.org/Manfre/django-mssql/src) running on windows.

We want allow user to filter data from some table ("Activity") via form, display it on the website's table and then show related data from another table ("Frames") on map.

class Frames(models.Model):
...

class Activity(models.Model):
frame_from = models.ForeignKey(Frames, ...)
...

The problem is: when we want to filter larger amount of data from Activity (let's say 200rows x 6 colums), we can not make other queries in the same request on table Frames (MARS is turned on in Django settings.py):

result = Aktywnosci.objects.filter(qset1) 

is always ok, but

path = Frames.objects.filter(qset2) 

when the previous query returned larger amount of data, raises OLE DB Error:

'Microsoft OLE DB Provider for SQL Server' Error: Cannot create new connection because in manual or distributed transaction mode.

PS. Database settings from settings.py:

# Database for this installation. 
DATABASES = {
'default':{
    'ENGINE': 'django.db.backends.sqlserver_ado', 
    'NAME': '***',                      
    'USER': '***',                      
    'PASSWORD': '***',                  
    'HOST': '***',                      
    'PORT': '',                        
    'OPTIONS' : {
        'provider': 'SQLOLEDB',
        'use_mars': True,                    
          }
}
}

PS2. I came across this issue on the google-code page of djang-mssql: http://code.google.com/p/django-mssql/issues/detail?id=79 - but it seems to be solved in new version of package...

What can I do about it?

Thanks in advance

Was it helpful?

Solution

We got the solution at bitbucket: https://bitbucket.org/Manfre/django-mssql/issue/13/ole-db-provider-for-sql-server-error from Michael Manfre - thanks a lot for this.

The solution is following:

"SQLOLEDB and MARS doesn't work very well and I intend on changing all of the documentation and defaults to assume a native client driver will be used. Try using the native client; "SQLNCLI10" or "SQLNCLI11".

DATABASES = {
    'default': {
        'ENGINE': 'sqlserver_ado',
        'NAME': 'mydb',
        'HOST': r'localhost',
        'USER': '',
        'PASSWORD': '',
        'OPTIONS': {
            'provider': 'SQLNCLI10',
            'extra_params': 'DataTypeCompatibility=80;MARS Connection=True;',
        },
    }
}

OTHER TIPS

Is "use_mars=True" set up in your "settings.py" file?

http://django-mssql.readthedocs.org/en/latest/settings.html

If this doesn't work, I have a question: is your selection in SQL Server involving tables with triggers on them (transact SQL scripts) - in this case the SQL Server will use a static cursor instead of a firehose one (which is what you need) therefore you will get your error. Try to get rid of the triggers, use some views in SQL Server and select from them instead of tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top