1

I'm using SQLAlchemy in WSGI python web app to query the database. If I do two concurrent requests, the second request invariably throws an exception, with SQL Server stating

[24000] [FreeTDS][SQL Server]Invalid cursor state (0) (SQLExecDirectW)

Unfortunately it looks like I can't use caching to prevent additional requests to the database. Is there another way to resolve this issue? Ideally using native python libraries (i.e. not relying on another python module)?

The only thing I can think of is using threads to put a lock on the function making the database queries, but I'm worried this will slow down the app.

Is there anything else that can be done? Is this a configuration issue?

I'm using FreeTDS v0.91 on a Centos 5.9 server, connecting to MS SQL Server 2008.

The webapp is based on Paste.

Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
  • Wondering if you ever came to a conclusion on how resolve this situation. I encountered the same thing where following the execution of a query any following queries give the error *Invalid cursor state (0) (SQLExecDirectW)*. In addition I noticed that the ResultProxy returned by query execution which does not seem to fail the first time has a rowcount of -1. – sonobenissimo Jun 22 '15 at 14:55
  • I did resolve it but can't remember how. I'm sorry I neglected to post the answer here. – Jordan Reiter Jun 22 '15 at 16:46

1 Answers1

1

Are your two concurrent requests using different database connections? DBAPI connections are not generally threadsafe. At the ORM level, you'd make sure you're using session per request so that each request has its own Session and therefore dedicated DBAPI connection.

zzzeek
  • 72,307
  • 23
  • 193
  • 185