Retain database object lock after commit

I select some object to update, and then perform an operation on it

obj = Model.objects.select_for_update().get(id=someID)
obj.somefield = 1
obj.save()

But I need to keep the FOR UPDATE lock on this object. The PostgreSQL documentation says that the lock FOR UPDATEwill be valid until the end of the transaction, which will be completed, since the save will commit. Even if I manage the commit manually, I need to save some information in the database (for this I need to commit).

So what can I do in this situation? If I select an object again, another process may make changes to that object before I install a new lock.

(I am using django 1.7 and postgresql 9.3)

+4
source share
1 answer

You cannot hold row locks after a transaction completes or rolls back.

This may be a reasonable precedent for advisory locks , although it is difficult to say with the limited information provided. Security locks can work at the session level, crossing transaction boundaries.

You cannot (ab) use a cursor WITH HOLD:

test=> DECLARE test_curs CURSOR WITH HOLD FOR SELECT * FROM dummy FOR UPDATE;
ERROR:  DECLARE CURSOR WITH HOLD ... FOR UPDATE is not supported
DETAIL:  Holdable cursors must be READ ONLY.

therefore, I think the advisory lock is pretty much your only option.


, , , . , , . - , , .

, concurrency , , - . , - , , , . , ORM, Hibernate, , Django concurrency, , , , .

+2

All Articles