E-Mail von Jason Hildebrand am 2003-02-24:

> This sounds interesting, but wouldn't a store have to hold the lock for
> the entire duration that it is running

It works like this:

  store=MyStore(**connectiondata)
  lock=store.get_lock("object %s" % someobject.serialNum())
  if lock.granted() :
     do_something
     store.saveChanges()
     store.clear()
     lock.release()

see code below,

MV


-------------------------------------------- snip

from MiddleKit.Run.MySQLObjectStore import MySQLObjectStore
import MySQLdb

class StoreLock :

    def __init__(self,s,store=None,timeout=10) :
        assert store
        self._store=store
        self._name=MySQLdb.escape_string(s)
        (co,cur)=store.executeSQL("Select GET_LOCK('%s',%s)" % (self._name,timeout))
        self._connection=co
        self._result=cur.fetchall()[0][0]

    def granted(self) :
        return self._result

    def release(self) :
        if self.granted() :
            (co,cur)=self._store.executeSQL("Select RELEASE_LOCK('%s')" % 
(self._name), connection=self._connection)
            self._result=0
            return cur.fetchall()[0][0]
        else :
            return 0

    __del__=release

class myStore(MySQLObjectStore) :

    def __init__(self,**kw) :
        MySQLObjectStore.__init__(self,**kw)


    def get_lock(self,s,timeout=10) :
        return StoreLock(s,store=self,timeout=timeout)

    def is_free_lock(self,s) :
        (co,cur)=self.executeSQL("Select IS_FREE_LOCK('%s')" % 
MySQLdb.escape_string(s))
        return cur.fetchall()[0][0]


-------------------------------------------------------------snip




-- Martin Virtel
[EMAIL PROTECTED]
aim / yahoo messenger mvftd
tel. +49 177 242 2889


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to