Custom Search

Tuesday, March 10, 2015

How to Django Debug TransactionManagementError: Transaction managed block ended with pending COMMIT/ROLLBACK

from django.db import transaction
from saju.core.models import Agent

1)
Error Case-1
===============

@transaction.commit_manually
def f1():
    print 1
    try:
        a=Agent.objects.filter(id=454200).all()[0]
        a.contact_email="bla@blabla.com"
        a.save()   
        raise Exception("dummy exception")
    except Exception as e:
        raise e
        transaction.rollback()   
    transaction.commit()

2)
Error Case-2
===============

@transaction.commit_manually
def f1():

    print 1
    try:
        a=Agent.objects.filter(id=454200).all()[0]
        a.contact_email="bla@blabla.com"
        a.save()
    except Exception as e:
        transaction.rollback()
    raise Exception("dummy exception")   
    transaction.commit()

3)
Solution-1
===============

###How to see the Actual error ?
###Add a "try..final" block with "transaction.rollback()" to see the Actual error

@transaction.commit_manually
def f1():

    try:
        print 1
        try:
            a=Agent.objects.filter(id=454200).all()[0]
            a.contact_email="bla@blabla.com"
            a.save()   
            raise Exception("dummy exception")
        except Exception as e:
            raise e
            transaction.rollback()   
        transaction.commit()
    finally:
        transaction.rollback()



4)
Solution-2
===============

###How to see the Actual error ?
###Add a "try..final" block with "transaction.rollback()" to see the Actual error
@transaction.commit_manually
def f1():

    try:
        print 1
        try:
            a=Agent.objects.filter(id=454200).all()[0]
            a.contact_email="bla@blabla.com"
            a.save()
        except Exception as e:
            transaction.rollback()
        raise Exception("dummy exception")   
        transaction.commit()
    finally:
        transaction.rollback()





No comments:

Post a Comment