question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

On MySQL backend, with the admin app enabled, if the admin view is in an atomic block, TransactionManagementError is raised.

See original GitHub issue

For this particular case we have installed:

  • Django==1.10.6
  • django-reversion==2.0.8
  • django-import-export==0.5.1
  • We use the django.contrib.admin app

In this particular project we are using these settings:

  • ATOMIC_REQUESTS: True for all databases
  • We have to add this middleware: reversion.middleware.RevisionMiddleware

When any admin view runs, these settings always run the view in an atomic block (in a transaction). Now, when importing a file with a Resource and an ImportExportModelAdmin in the admin app, we always get this error: TransactionManagementError: This is forbidden when an 'atomic' block is active..

This is because import_export.resource.Resource.import_data() tests for connection.features.supports_transaction. This is OK on most RDBMS, but on MySQL the support for transactions depends on the database engine being used, so Django runs a test to see if the DB supports transactions. During the test, a temporary table is created and also calls set_autocommit(False). That call can only be executed outside of an atomic block (Django code test for in_atomic_block for the DB connection).

This also has the side effect the table created by Django (called ROLLBACK_TEST) to test transaction support is never deleted from the DB, because the rollback to the savepoint marked by Django is never performed.

I’ll submit a Pull Request with a short patch to prevent this: if the import_data() is running inside an atomic block, then we know the DB supports transactions and we don’t test for connection.features.supports_transaction.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bmihelaccommented, Dec 1, 2017

great news, thank you 😃

1reaction
bmihelaccommented, Nov 30, 2017

Hello @camilotorresf, @int-ua

thanks for your report. I have make some changes to transactions handling.

It would be great if you could check if problem persists when using refactor-transaction-handling branch that is available on https://github.com/bmihelac/django-import-export/tree/refactor-transaction-handling

If there are still problems it would be very useful to create test case or at least minimum application that can be shared on github and where issue can be easily reproduced.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TransactionManagementError "You can't execute queries until ...
For me, I already had a transaction.atomic() block, but I got this error and I had no idea why. I took this answer's...
Read more >
Database transactions | Django documentation
When ATOMIC_REQUESTS is enabled, it's still possible to prevent views from ... of the database connection within an atomic block will raise an...
Read more >
PDF - Django Documentation
Django has a lot of documentation. A high-level overview of how it's organized will help you know where to look for certain things:....
Read more >
About bhara - Cloudera Community
Hi @stevenmatison , I was able to run the same query from hive ,but from hue i see database lock . When i...
Read more >
[Solved]-django: commit and raise inside transaction.atomic()
Besides the answer already posted, in case of having nested methods where you start the atomic block somewhere higher in the chain, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found