> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] Behalf Of Ian
> Bicking
> Sent: Thursday, April 08, 2004 11:23 PM
>
> On Apr 9, 2004, at 12:14 AM, Shayne ONeill wrote:
> > Then webware starts threading it. Now at this stage I'm *convinced* the
> > cursor is NOT threadsafe.
>
> Absolutely it is not threadsafe.  MySQL connections are not threadsafe
> either, I believe.  If that's really the problem, then the error is
> simply that the only way the application has been tested for
> concurrency is by hitting the submit button multiple times; but in any
> real situation the application will die very quickly.
>
> > Theres good reason too. A db cursor essentially keeps stateful info on
> > where the querys and stuff are 'at'.
> > So if thread #1 is in a diferent stage of life cycle and using the
> > cursor
> > then if thread #2 molests the cursor, it'll upset thread #1.
> >
> > The answer is to either aquire a threadlock around cursor usage , or if
> > that does not work, then use multiple cursors (which might punish
> > mysql.
> >
> > Now If I understand correctly somewhere out there there is a db cursor
> > pooling library. That might hold the key.
>
> There's a DBPool class in Webware, somewhere (MiscUtils?).  It handles
> this.
>

Second that! Both MySQL connections and cursors are not thread-safe.

>>> import MySQLdb
>>> MySQLdb.thread_safety()
1

>From http://www.python.org/peps/pep-0249.html (the Python DB API-2.0 spec
linked from http://www.python.org/topics/database/):
'''
threadsafety

    Integer constant stating the level of thread safety the
    interface supports. Possible values are:

        0     Threads may not share the module.
        1     Threads may share the module, but not connections.
        2     Threads may share the module and connections.
        3     Threads may share the module, connections and
              cursors.

    Sharing in the above context means that two threads may
    use a resource without wrapping it using a mutex semaphore
    to implement resource locking. Note that you cannot always
    make external resources thread safe by managing access
    using a mutex: the resource may rely on global variables
    or other external sources that are beyond your control.
'''

Another important note about MySQLdb is that connections and cursors should
be explicitly closed to avoid memory leaks. Forgetting to close connections
can also lead to all available connections to the server being used up
pretty darn fast. Letting Python garbage collect the connection object is
not good enough.

- Ben




-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to