[ How come my Django code doesn't rollback changes when there is an error? ]
Below is my code. (It's run by Celery, but I don't think that's important.) When an error occurs, the mongo record gets removed (yay!) but the .rollback() does not work, so mysql keeps the record instead of rolling it back. Why?
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
Answer 1
Because you're not using the InnoDB table engine, which, unlike MyISAM, supports transactions.