user1934017
user1934017

Reputation: 3

Why doesn't manual transaction rollback work in Django?

My environment is python2.7.3 and django1.4.1

I am using MySQL with MyISAM tables.

my test code:

from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)

from myproject.models import *
from django.db import transaction

@transaction.commit_manually
def test_trans():
    r=tab1.objects.get(no="1")
    r.value=100
    r.save()
    transaction.rollback()#I already rollback here, but data is still being updated to the database

def main():
    try:
        test_trans()
    except:
        pass
if __name__ == '__main__':
    main()

It seems that transaction.rollback() is not rolling back my transaction, as data is still being updated to the database.

Upvotes: 0

Views: 463

Answers (1)

Steve Mayne
Steve Mayne

Reputation: 22818

If you are using MySQL with MyISAM, it doesn't support transactions. Change your tables to InnoDB if you want to use this functionality.

I add the following to my settings.py to do this automatically (when calling syncdb):

DATABASE_OPTIONS = {"init_command": "SET storage_engine=INNODB"}

Upvotes: 2

Related Questions