What is the correct way to recover from IntegrityErroror any other errors that could lead to the loss of my transactions without using manual transaction management?
In my application, I ran into problems with IntegrityErrorwhich I want to recover, which will bring later database activity, leaving me with:
DatabaseError: current transaction is aborted, commands ignored until end of transaction block`
for all database operations after ignoring IntegrityErrors.
This code block should reproduce the error that I see
from django.db import transaction
try:
MyModel.save()
except IntegrityError:
pass
MyModel.objects.all()
According to the docs , the solution to recover after IntegrityErroris to roll back the transaction. But the following code leads to TransactionManagementError.
from django.db import transaction
try:
MyModel.save()
except IntegrityError:
transaction.rollback()
MyModel.objects.all()
EDIT: TransactionManagementError, except a:
connection._cursor().connection.rollback()
django transaction.rollback(), MyModel.objects.all() , , " ". , , (, , , autocommit), , .
№ 2: , , ? , autocommit, , .
EDIT # 3:. , django 1.4 ( ) , Model.objects.bulk_create() autocommit.
:
- Django: 1.4 (
TransactionMiddleWare ) - Python: 2.7
- Postgres: 9.1