views:

197

answers:

1

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.

Thanks and regards, Ramya

+1  A: 

Just to make sure, you're using strings for primary keys if I understand correctly?

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

This means there's an error in some Python code. Have you tried using another version or revision of Django or searching the Django trac for your bug? It isn't so uncommon to be affected by some bug if you're using version from trunk.

As an alternative you could also try to deploy Django using multiple processes instead of multiple threads if that's an option.

However, you might still want to find out why you're getting duplicate requests as it might uncover some other bug.

Yup.. Thanks a ton.. I was using (1, 0, 2, 'final', 0).. I moved to version (1, 1, 0, 'final', 0) and the errors disappeared..
Ramya