am trying to do a transactional tasks whereby the task will rollback the database updates if it fails to send the email.
Below is my code, any one can advice what am doing wrong here?
from celery.task import task
from django.core.mail import send_mail, send_mass_mail
from django.db import transaction
@task(name='communicator.process_emails')
@transaction.commit_manually
def process_emails():
from models import Comm_Queue
try:
message = []
for i in Comm_Queue.objects.filter(status='P').order_by('sender_email'):
message.append((i.subject, i.content, i.sender_email, [i.recipient_email]开发者_如何学JAVA))
Comm_Queue.objects.filter(id=i.id).update(status='S')
if send_mass_mail(message):
transaction.commit()
except Exception, e:
print 'rolled back (exception): %s' % e.__str__()
transaction.rollback()
Since you're using MySQL, the first thing you need to check is whether the database engine you're using supports transactions in the first place and whether transactions have been enabled in MySQL configuration.
See this link for more information on MySQL/Django transaction issues: https://docs.djangoproject.com/en/dev/ref/databases/#storage-engines
精彩评论