Opened 4 years ago

Closed 20 months ago

#22309 closed Cleanup/optimization (wontfix)

DatabaseFeatures.supports_transactions should use temporary tables

Reported by: lucastan@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I am using South 0.8.4 (refer to source

The file south/db/ has this function has_ddl_transactions which access django.db.connection.features.supports_transactions

However, upon accessing, it gives an error. The error could be reproduced by the following:

./ shell

>>> from django.db import connection
>>> connection.features.supports_transactions

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/xx/venv/local/lib/python2.7/site-packages/django/utils/", line 49, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/xx/venv/local/lib/python2.7/site-packages/django/db/backends/", line 664, in supports_transactions
  File "/home/xx/venv/local/lib/python2.7/site-packages/django/db/backends/", line 315, in leave_transaction_management
    "Transaction managed block ended with pending COMMIT/ROLLBACK")
TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK

Using south 0.8.4 and django 1.6. Seems like a Django error? But in the first place, is it safe to access features attribute?

Change History (9)

comment:1 Changed 4 years ago by anonymous

I want to point that for SQLite backend, there is no error. But for MySQL, there is an error.

comment:2 Changed 4 years ago by Tim Graham

I cannot reproduce this using the steps provided and connecting to a MySQL database. Perhaps you could provide a minimal project to reproduce?

comment:3 Changed 4 years ago by Tim Graham

Resolution: needsinfo
Status: newclosed

comment:4 Changed 4 years ago by anonymous

Ok, I have resolved this issue. The whole problem is that the supports_Transactions method actually creates a table and drop it to test transaction. Due to the privileges, it couldn't drop a table , resulting in leave_transaction_management throwing an error due to an incomplete transaction.

So my question is: is the supports_Transactions method safe for use in a multi-process django app, since multiple threads could create and drop the same table concurrently? I suggest to create a unique name for the table based on UUID or some sort.

comment:5 Changed 4 years ago by Anssi Kääriäinen

We should use temporary tables. They are connection local, and in any case using temporary table is a lot better than using normal tables. Temporary tables are supported from MySQL 3.23 onwards, so See The only question is if temporary tables have different transaction rules than normal tables. I don't see anything indicating that this is the case, but then again when dealing with MySQL it wouldn't be a surprise if this is the case.

So, needed here:

1) check that temp tables work like normal tables when it comes to transaction management. My understanding is that the default engine in use for MySQL affects if transactions are usable, so the check would be to switch to using MyISAM tables as default and then verify that temp table gives the same result as normal tables.
2) create temp table doesn't commit the current transaction - create table does. This might change the behavior of the method.
3) check that this doesn't affect other backends (sqlite seems to use the supports_transactions method, too)
4) write a patch

Also, the supports_transactions method seems scary. If ran inside transaction the end result is silent rollback + switch to autocommit mode. So, step 5) is to verify when exactly this method is ran, and also make the method assert that no transaction is open when it is ran. If it is possible that this method will get ran inside transaction and when not testing then we should likely backpatch the fix, too, as this is a clear data loss issue in that case.

comment:6 Changed 4 years ago by Anssi Kääriäinen

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:7 Changed 4 years ago by Tim Graham

Summary: error accessing django.db.connection.features?DatabaseFeatures.supports_transactions should use temporary tables

comment:8 Changed 20 months ago by Tim Graham

Resolution: needsinfo
Status: closednew
Type: BugCleanup/optimization
Version: 1.6master

comment:9 Changed 20 months ago by Tim Graham

Resolution: wontfix
Status: newclosed

#26541 eliminated the need to use table creation for DatabaseFeatures.supports_transactions on MySQL, so this looks obsolete.

Note: See TracTickets for help on using tickets.
Back to Top