为什么我的Django代码在出现错误时不会回滚更改?

时间:2011-01-27 02:51:45

标签: python mysql database django

以下是我的代码。 (它由Celery运营,但我认为这不重要。) 当发生错误时,mongo记录被删除(yay!)但.rollback()不起作用,因此mysql保留记录而不是回滚。为什么呢?

class Submitter(Task):
    @transaction.commit_manually
    def run(self, post, **kwargs):
        docDB = h.connect_mongo(collection="posts")

        post = cPickle.loads(post)
        if post.has_key("original_file"):
            try:
                post = docProcessor.process_images(post)
            except:
                traceback.print_exc()
        post['processed'] = True

        #START INSERT -------
        try:
            mongo_id = docDB.insert(post)
            author = User.objects.get(id=post['author_id'])

            #Sync mysql content with mongo id
            c, created = Content.objects.get_or_create(mongo_id = str(mongo_id), author=author)
            c.save()

            #now update the content_id
            post['content_id'] = c.id
            post['_id'] = mongo_id
            updated = docDB.save(post)

            #finally, add a vote to the document.
            h.insert_vote(content_id = c.id, lat = post['loc_latlong'][0],
                     long = post['loc_latlong'][1],
                     ip = post['ip'], thevote = 1, theuser = author, mongo_con = docDB)

            transaction.commit()
        except:
            transaction.rollback()
            docDB.remove(post)
        #END INSERT --------

        return True

1 个答案:

答案 0 :(得分:3)

因为您没有使用InnoDB表引擎,与MyISAM不同,它支持事务。