Python 为什么我的Django代码没有';出现错误时是否回滚更改?
下面是我的代码。(它是用芹菜经营的,但我认为这并不重要。) 当发生错误时,mongo记录会被删除(耶!),但是.rollback()不起作用,所以mysql会保留记录而不是回滚。为什么?Python 为什么我的Django代码没有';出现错误时是否回滚更改?,python,mysql,database,django,Python,Mysql,Database,Django,下面是我的代码。(它是用芹菜经营的,但我认为这并不重要。) 当发生错误时,mongo记录会被删除(耶!),但是.rollback()不起作用,所以mysql会保留记录而不是回滚。为什么? class Submitter(Task): @transaction.commit_manually def run(self, post, **kwargs): docDB = h.connect_mongo(collection="posts") post
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
因为您没有使用InnoDB表引擎,它与MyISAM不同,支持事务。因为您没有使用InnoDB表引擎,它与MyISAM不同,支持事务。您能告诉我们发生了什么错误吗?为了测试这段代码,我特意引入了一些错误的东西。然后,异常命中。它应该回滚(),但mysql不会回滚。但Mongo确实删除了该帖子。没有错误。你能告诉我们发生了什么错误吗?为了测试这段代码,我特意引入了一些错误的东西。然后,异常命中。它应该回滚(),但mysql不会回滚。但Mongo确实删除了该帖子。没有错误。你怎么知道他用的是哪个DB引擎?你怎么知道他用的是哪个DB引擎?