【发布时间】:2011-01-27 02:51:44
【问题描述】:
下面是我的代码。 (它由 Celery 经营,但我认为这并不重要。) 当发生错误时,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
【问题讨论】:
-
你能告诉我们发生了什么错误吗?
-
为了测试这段代码,我故意引入了一些错误的东西。然后,异常发生了。它应该回滚(),但mysql不会回滚。不过,Mongo 确实删除了该帖子。没有错误。它只是去。
标签: python mysql database django