logo       

Re: Threaded application + IntegrityError: msg#02307

DjangoUsers

Subject: Re: Threaded application + IntegrityError

On Fri, Jul 31, 2009 at 12:41 PM, ramya <mail.ramya@xxxxxxxxx> wrote:

Hi,
I have python threaded application + Postgres. I am using Django's ORM
to save to Postgres..
I have concurrent save calls. Occasionally 2 threads save with the
same primary key which leads to an issue.

Postgres log:
ERROR: Âduplicate key value violates unique constraint "store_pkey"
STATEMENT: ÂINSERT INTO "store" ("store_id", "address") VALUES
(E'HAN277', E'101 Ocean Street')

Code:
In the code I see an IntegrityError. I tried different ways to handle
this.

a.
try:
Âa.save()
except IntegrityError:
Âpass

This causes InternalError

b. Tried to do transaction roll back.. but not sure.. As far as I
understand you need to distinct save calls to have transactions

    Âsid = transaction.savepoint()
    Âtry:
     Ârow.save()
    Âexcept IntegrityError, e:
     Âtransaction.savepoint_rollback(sid)
     Âpass
    Âtransaction.commit()
The first savepoint fails with

AttributeError: 'NoneType' object has no attribute 'cursor'


a. I read somewhere django is not 100% thread safe. Is it a good
choice in my usecase. I was already using Django for other application
and need an ORM.. So naturally I chose Django
b. How to handle this situation.. Any comments.

One trick you could try is:

try:
Âa.save()
except IntegrityError:
Âwait(.1)
Âa.save()

This generally only works for lightly used multi-threaded application, and you should be able to embed the Try-Except blocks to try more than twice.

Â


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to django-users+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Google Custom Search

News | Mail Home | sitemap | FAQ | advertise