[mod_python] Tips and Tricks initiative

Bob Ippolito bob at redivi.com
Wed Jul 3 18:36:04 EST 2002

It depends on the particular db module, some modules have threadsafe 
database instances, some don't.  Some have threadsafe database cursors, 
and some don't.  Read the docs.  If it's DBAPI compliant, check its 
thread safe level.  I have never used anything that's thread safe level 
3, most are level 2 or 1 in my experience.

from http://www.python.org/topics/database/DatabaseAPI-2.0.html
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 
bymanaging access using a mutex: the resource may rely on global 
variables or other external sources that are beyond your control.


On Wednesday, July 3, 2002, at 06:38 PM, vio wrote:

> * Alain Tesio <alain at onesite.org> [020703 17:11]:
>>> For instance, I'd be very interested to read in such a tricks and 
>>> tips list how to avoid the
>>> overhead of creating a database connection at each call to 'handler'. 
>>> And so on.
>> Just make the connection a global variable.
> I think I tried that, initialized the db connection at the module 
> level. But somehow my cursor got corrupted, or something. I forgot 
> oracle's error number, only that my workaround was to do something like 
> (we're at module level here):
> db = DCOracle2.connect(my_login_string)
> def get_db_cursor():
>     "create database connection"
>     cursor = db.cursor() # Allocate a cursor
>     return cursor
> In fact I see that I am re-using the same connection here, so let me 
> re-phrase my initial question: how do I re-use the same *cursor* (or it 
> this a bad idea in the first place). I wonder what would happen in such 
> a multi-threaded environment (well, I'm saling in Linuxland) where 
> multiple httpd instances will hit on the db server (ora8i in my case) 
> with the same cursor *concurrently* (never got to that point of 
> desperation, though - but just from contemplating such possibility 
> sounds like total anarchy). What I did try but eventually got an error 
> on was to re-use the same cursor sequentially (in a non-threaded 
> application).
> Cheers, Vio
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list