Do cursors in Django run inside the open transaction?

Viewed 5228

My Django application is using some custom SQL which I am executing inside a view like this:

db = router.db_for_write(model)
cursor = connections[db].cursor()
cursor.execute("INSERT INTO ....")

Since I am using the TransactionMiddleware, my view is running inside a transaction, but I'm not clear if getting a new cursor like this "escapes" the currently open transaction or if the cursor is still a part of the open transaction. I am getting some error messages that lead me to believe that cursor is running inside the transaction.

I would like to be able to use a cursor to execute SQL commands outside of the transaction that was opened by the TransactionMiddleware. Is this possible?

If it matters, I am running Django 1.4 with a PostgreSQL 8.4 database.

2 Answers
Related