On Fri, Jul 31, 2009 at 12:41 PM, ramya <mail.ra...@gmail.com> 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@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to