Opened 9 years ago

Closed 9 years ago

#22947 closed Uncategorized (invalid)

transaction rollback not working in django 1.6.5

Reported by: khaihkd Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords: transaction rollback
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

Example:
my model: User (id: integer auto increment, name: string, age: integer)
I have a array data insert to database (database is mysql)

[[[['John', 20], ['Bob', 30], ['Mark', 'not number']]]]
@transaction.non_atomic_requests()
def update_Ac_Point_Detail(self):
   array = [[[['John', 20], ['Bob', 30], ['Mark', 'not number']]]]
   transaction.set_autocommit(False)
   for item in array:
      try:
          user = User()
          user.name = item[0]
          user.age = item[1]
          user.save()
       except:
          transaction.rollback()
          return
   transaction.commit()

When I check in database, it has 2 new records. it's not rollback.
Please help me. Thank you very much

Change History (3)

comment:1 Changed 9 years ago by Aymeric Augustin

Can you confirm that you're using the InnoDB storage engine? MyISAM doesn't support transactions.

comment:2 Changed 9 years ago by khaihkd

I'm using InnoDB storage engine. and I try django version 1.4 1.5 1.6, it not working.

Last edited 9 years ago by khaihkd (previous) (diff)

comment:3 Changed 9 years ago by Aymeric Augustin

Component: Python 2Database layer (models, ORM)
Description: modified (diff)
Resolution: invalid
Status: newclosed
UI/UX: unset

There's at least one problem with your example: the method you're decorating with transaction.non_atomic_requests isn't a Django view (i.e. a callable that accepts a request and returns a response).

I've converted into a working test case, and that test passes under MySQL.

from django.contrib.auth.models import User
from django.db import transaction
from django.test import TransactionTestCase

class Ticket22947Test(TransactionTestCase):

    available_apps = ['django.contrib.auth', 'blabla']

    def test_ticket_22947(self):
        self.assertEqual(0, User.objects.count())
        array = [[[['John', 20], ['Bob', 30], ['Mark', 'not number']]]]
        transaction.set_autocommit(False)
        for item in array:
            try:
                user = User()
                user.name = item[0]
                user.age = item[1]
                user.save()
            except:
                transaction.rollback()
        transaction.commit()
        transaction.set_autocommit(True)
        self.assertEqual(0, User.objects.count())

Please see TicketClosingReasons/UseSupportChannels for further help on this issue.

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