zodb: database conflict fail

oemsoftbests

New Member
I have a server, and a client.A client sends a request. The request has a certain key associated with it, e.g. \[code\]a-1\[/code\], \[code\]a-2\[/code\], \[code\]b-1\[/code\], \[code\]b-4\[/code\].If two requests for the same key come in at once, there will be a conflict error, as the same data structures are being modified.I can adapt the client to simply not send two requests of the same key at once. However, I'd like this system to work with multiple clients, too. It seems silly to have the clients coordinate what they send to the server. Instead, I'd like the server to simply block on a request of a certain key if that key is already being modified, until the other requests with that same key are done.To this end, I've created a locking system. At the beginning of the function on the server, I do:\[code\]key = ...print "Acquiring %s lock..." % (key,)KEY_LOCKS[key].acquire()print "%s lock acquired." % (key,)def after_commit_hook(success): KEY_LOCKS[key].release() print "(after %s commit): Released %s lock" % (('failed', 'successful')[success], key)transaction.get().addAfterCommitHook(after_commit_hook)\[/code\]where \[code\]KEY_LOCKS\[/code\] is a dict mapping keys to \[code\]threading.Lock\[/code\]s. Afterwards follows the code that modifies the persistent data structures.What I assume would happen is that, if a request comes in for a key that's already being processed, it would block when acquiring the lock. Only when the earlier request has already been committed (thus being beyond any conflict errors), would the new request resume. The requests do nothing that would conflict until the lock is acquired.Most of the requests work fine:\[code\]Acquiring a-b lock...a-b lock acquired.(after successful commit): Released a-b lockAcquiring a-c lock...a-c lock acquired.(after successful commit): Released a-c lock\[/code\]However, there is still an issue when the same key is sent, even though the locking seems to work:\[code\]Acquiring q-q lock...q-q lock acquired.Acquiring q-q lock...(after successful commit): Released q-q lockq-q lock acquired.(after failed commit): Released q-q lockrepoze.retry retrying, count = 1Traceback (most recent call last):...ConflictError: database conflict error (oid 0x13009b, class persistent.list.PersistentList)\[/code\]And then the request retries. Note that the \[code\]q-q lock\[/code\] was only acquired after the successful commit. What gives? Why is this system not preventing conflict errors? Where is my assumption incorrect?EDIT: Well, if, before the \[code\]transaction.get().addAfterCommitHook(after_commit_hook)\[/code\] line I put \[code\]transaction.begin()\[/code\], it works. For the life of me I can't figure out why. Before the \[code\]transaction.begin()\[/code\] line, the entirety of my code is:\[code\]post = request.paramsif not post: return Response("No data!")data = http://stackoverflow.com/questions/9810116/eval(post['data'])time_parsed = time.time() my_app = request.context\[/code\]This solves my problem but I'm not putting that as an answer 'cause I still want to know: Why does it give conflict errors if I don't start a fresh transaction right before?
 
Top