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

2024-04-26 17:19:47 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我的代码。(这是用芹菜做的,但我不认为这很重要。) 当发生错误时,mongo记录将被删除(耶!)但是.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

Tags: idtruemongo记录mysqlcontentpostauthor